|
C'mon, that's just the C# way of saying
On Error Resume Next
|
|
|
|
|
Nemanja Trifunovic wrote: C'mon, that's just the C# way of saying
On Error Resume Next
Come to think of it, almost any VB6 error handling code would belong here.
Nathan
|
|
|
|
|
I wish it was that simple.
|
|
|
|
|
Actually it's worse, more like On Error Goto ...
WarePhreak
Programmers are tools to convert caffiene to code.
|
|
|
|
|
Oddly enough, I ran into a place where I have to use this in a WPF app I'm working up for my boss. In code, if you try to load an Image control, you can't just give it a filename, you have to give it an ImageSource object. The most obvious of these is BitmapSource, which inherits from ImageSource. BitmapSource won't take a filename either - it requires a Uri object, which is (as its name implies) a URI to a file somewhere.
BitmapSource has one strange little caveat. Its properties are immutable. Nothing you change will stick unless you explicitly call the BeginInit method on it. Once you're done, call EndInit and all your changes are persisted.
Herein lies the problem. My application calls BeginInit on a BitmapSource and runs through a switch that looks at the application state to decide which image to load. If the switch detects an invalid state, it sets the Uri to null (so that no images load). Then, at the end of the switch, I call EndInit and my BitmapSource should have loaded either an image file or nothing at all.
Here's the kicker. BitmapSource throws an exception if you call EndInit without setting a Uri. (Well, a Uri or a StreamSource, but I'm not dealing with streams in my app.) So in the case where the switch didn't load an image in its default case, my BitmapSource blows up.
Unfortunately, whoever designed the interface that ImageSource implements (the one that contains BeginInit and EndInit) didn't seem to realize this fact, and neglected to include an AbortInit method. In other words, once you call BeginInit, you have to load an image somehow or catch the error that results from an empty URI.
So yeah, my method now looks like this:
imgLoader.BeginInit();
switch(stateVar)
{
default:
imgLoader.UriSource=null;
break;
}
try
{
imgLoader.EndInit();
}
catch { }
Sad, isn't it?
Please don't bother me... I'm hacking right now. Don't look at me like that - doesn't anybody remember what "hacking" really means?
|
|
|
|
|
I might be misunderstanding, but shouldn't you still have some code in the catch block to ensure that your stateVar variable is invalid. Otherwise you won't know about exceptions when the exception is meaningfull?
|
|
|
|
|
Not really; the only invalid stateVar that should get caught by the default case is 0 (i.e. don't display any images). There's a handler that should set the Image object to null, rather than its URI in this case, but there's still a couple of cases where the application changes to state 0 and runs the switch statement instead. (These cases shouldn't exist but I didn't write up a full state diagram because this application is a tech demo my boss wants to present to his peers and he doesn't want me to spend a lot of time on it. Go figure.)
Please don't bother me... I'm hacking right now. Don't look at me like that - doesn't anybody remember what "hacking" really means?
|
|
|
|
|
Yes, but you had no choice, this guy put it in a block for filling part of dataSet. He was just lazy (or had no clue what he was doing).
|
|
|
|
|
It just occurred to me now, to replace the empty catch with an insulting error message. That will change management's view that proper exception handling is a waste of time.
Calling all South African developers! Your participation in this local dev community will be mutually beneficial, to you and us.
|
|
|
|
|
it happens.
primarily because not everything thrown is worth worrying about. sometimes just knowing the operation failed is enough.
int SafeParse(string bar)
{
int p = 0;
try
{
p = int.Parse(bar);
}
catch () {}
return p;
}
maybe i don't care to know if the string wasn't a number, because zero is a perfectly adequate default value and it's not worth bothering the user about.
|
|
|
|
|
unfortunately, <<tryparse>> isn't implemented in all types...
(yes|no|maybe)*
|
|
|
|
|
Why not use IsNumeric to avoid the error
Is ignoring the error less stressfull that testing bar?
Quote from Great Outdoors:
its a confident traveller who farts in India
|
|
|
|
|
that's an example; not the only time it happens.
|
|
|
|
|
Private Sub donothing(param as Object)
End Sub
|
|
|
|
|
I must say it is well-self-documented-code!
Anyway, IMHO, NULL operations are useful at times.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
bigbrownbeaver wrote: What does it do
The name of the method itself explains it.
Vasudevan Deepak Kumar
Personal Homepage Tech Gossips
Yesterday is a canceled check. Tomorrow is a promissory note. Today is the ready cash. USE IT.
|
|
|
|
|
|
I know one better.
void Class::Function()
{
bool result = true;
try
{
}
catch(...)
{
result = false;
}
return result;
}
Nuclear launch detected
|
|
|
|
|
That's an option.
If the Lord God Almighty had consulted me before embarking upon the Creation, I would have recommended something simpler.
-- Alfonso the Wise, 13th Century King of Castile.
|
|
|
|
|
Uh..... that code wouldn't even pass the compiler. You can't return a boolean value in a method declared as type "void".
|
|
|
|
|
True... the return type was boolean.
But I posted the example to see how much code and runtime overhead you need to return true from a function that does nothing.
Nuclear launch detected
|
|
|
|
|
A few days ago, i have to update our prelife server with the new code.
The code itself was written in asp (not asp.net) and developed month ago.
After updating and running some manual tests, we decided that this code works fine.
But we missed something very important...
Take a look in this code and think about what happend, after strSQL gets the value Null (database NULL)...
Function SafeSQLString2(byval strSql)<br />
<br />
if len(strSQL) > 0 then<br />
strSql = Replace(strSql, "'", "")<br />
end if<br />
<br />
SafeSQLString2 = strSql<br />
end function
The workmate who wrote this code isn't available anymore.
At the moment i'm rewriting the complete code and removing all of this trash.
|
|
|
|
|
I hope you're using parameterized queries[^] in your new code. There's no need to eliminate quote marks in a field if you use parameters.
DoEvents : Generating unexpected recursion since 1991
|
|
|
|
|
At the moment i'm banging my head at the wall.
I'm thinking about deleting this piece of source and write it from scratch.
But thanks for the link.
If i ever get the permission to redesign the complete application, the first thing i will do is switching from ASP to something other. At the moment i prefer C#.
|
|
|
|
|
I was responsible for this horror:
Version 1.0 I had the following:
const DWORD ACCESS_NONE = 0;
const DWORD ACCESS_VIEW = 1;
const DWORD ACCESS_MODIFY = ACCESS_VIEW | 2;
const DWORD ACCESS_DELETE = ACCESS_VIEW | 4;
const DWORD ACCESS_ALL = ACCESS_VIEW | ACCESS_MODIFY | ACCESS_DELETE;
const DWORD ACCESS_ADMIN = ACCESS_ALL;
I needed ACCESS_EXECUTE in the next version of the software. So I declared:
const DWORD ACCESS_EXECUTE = 8;
const DWORD ACCESS_ALL = ACCESS_VIEW | ACCESS_MODIFY | ACCESS_DELETE | ACCESS_EXECUTE;
const DWORD ACCESS_ADMIN = ACCESS_ALL;
Where is the horror? We promised the user that old databases and files will run without any changes.
Co-Author ASP.NET AJAX in Action
CP Quote of the Day:
It is the same Friday that blooms as a new enriching day with novelty and innovation for us every week. - Vasudevan Deepak Kumar
|
|
|
|