Comment On You Can Never Be Too Cautious

At least, that's what Subhash Gopalakrishnan's colleauge believes. He just simply does not trust the standard method of exception handling .... ever. Maybe he's had a bad experience with Java ... or maybe ... he knows something about Java that we don't ... [expand full text]
« PrevPage 1 | Page 2Next »

Re: You Can Never Be Too Cautious

2005-01-20 13:50 • by
Actually, catch(Exception e) does not catch everything that can be thrown...

Re: You Can Never Be Too Cautious

2005-01-20 13:55 • by Bellinghman
So, the logic is as follows:

1 Set the flag to true.
2 Do the work
3 Set the flag to false when the work has been done

If any failure occurs during step 3, the work has succeeded, so silently swallow the exception.

Re: You Can Never Be Too Cautious

2005-01-20 14:04 • by Razzie
28212 in reply to 28210

:
Actually, catch(Exception e) does not catch everything that can be thrown...


It doesn't? That's new to me. I'd love to see an example.


I still think this is a major wtf. Why on earth would you want to do it this way... me wonders...

Re: You Can Never Be Too Cautious

2005-01-20 14:16 • by
28215 in reply to 28212
Exception is a subclass of Throwable, so anything that is a Throwable
but not an Exception can be thrown and won't be caught by
catch(Exception e). In practice this is limited to subclasses of Error,
which are not generally things one should catch anyway.

Re: You Can Never Be Too Cautious

2005-01-20 14:27 • by Scott
28216 in reply to 28212
Razzie:

  wrote:
Actually, catch(Exception e) does not catch everything that can be thrown...


It doesn't? That's new to me. I'd love to see an example.


I still think this is a major wtf. Why on earth would you want to do it this way... me wonders...

Re: You Can Never Be Too Cautious

2005-01-20 14:29 • by Scott
28217 in reply to 28216
Hmm, I wrote an example the first time.  I guess the system
doesn't actually want people to quote other people.  Here it is
again.  In asp.net:



try

{

    Response.Redirect("YourMomIsHot.html");

}

catch(Exception e)

{

    //this never gets hit

}

Re: You Can Never Be Too Cautious

2005-01-20 14:33 • by RJ
28218 in reply to 28215
It wouldn't matter anyway.  If the catch lets one slip past, you'll continue the unwind and skip all the flag nonsense.



My only thought is that this dude doesn't realize you can put a throw
inside a catch block without making the computer catch fire  That
doesn't explain why he checks the flag inside the catch block
though....  Strange code...



Maybe some messed up implementation of checked exceptions is forcing this monstrosity?

Re: You Can Never Be Too Cautious

2005-01-20 14:34 • by skicow
28219 in reply to 28215

:
Exception is a subclass of Throwable, so anything that is a Throwable but not an Exception can be thrown and won't be caught by catch(Exception e). In practice this is limited to subclasses of Error, which are not generally things one should catch anyway.


True.


But it's still a WTF since he's basically not trusting the catch to only fire when an Exception has been thrown - he tests the Boolean before executing any code within the catch block....

Re: You Can Never Be Too Cautious

2005-01-20 14:37 • by Spaceman Spiff
You can never be too sure.  He's just checking to see if it is REALLY REALLY an exception.

Re: You Can Never Be Too Cautious

2005-01-20 14:58 • by Drak
28222 in reply to 28220

Perhaps there was some code behind the setting of the flag to false where it didn't matter if an exception was thrown (like trying to send an email or something, who cares if that fails?). Still, I would personally put that in two separate try/catch constructions.


The .Net code that's above here in the forum somewhere probably gives a 'Thread was being aborted' exception. This is not caught because it happens in the logic of the try/catch. Not sure if putting ANOTHER try/catch around it would help, cos chances are it would generate the same error. You can catch it though in the global.asax [:P]


Drak

Re: You Can Never Be Too Cautious

2005-01-20 15:32 • by
28228 in reply to 28217
Scott:
Hmm, I wrote an example the first time.  I guess the system
doesn't actually want people to quote other people.  Here it is
again.  In asp.net:



try

{

    Response.Redirect("YourMomIsHot.html");

}

catch(Exception e)

{

    //this never gets hit

}





can we please avoid another discussion of Request.Redirect? We've been over this one a few times before I believe...

Re: You Can Never Be Too Cautious

2005-01-20 15:46 • by
28230 in reply to 28228
Can we move on to Context.Response.Redirect then? [8-|]



Cheers

Re: You Can Never Be Too Cautious

2005-01-20 15:51 • by znaps
Apart from the general WTFness of it, the worst thing is that if an
Exception occurs, he throws it away with any useful info that it might
provide, replacing it with a generic "An Error has Occurred" error
which tells nobody anything about what happened.

Re: You Can Never Be Too Cautious

2005-01-20 15:56 • by

His coworker must be an old VB 4/5/6 developer... what's they've done is create the Java equivalent of "On Error Goto" the end of the try catch block


If the ExceptionFlag is set the false, then no stacktrace will ever be printed, and no "An Error has occured" exception will ever be thrown. it will simply "resume next" at the end of the try catch block. [;)]

Re: You Can Never Be Too Cautious

2005-01-20 16:09 • by

WTF.  Doesn't this guy know exceptions are bad for performance?

public string foo()
{
boolean
exceptionFlag = true;
try {
initializeResources();
compareAggregates();
exceptionFlag = false;
} catch(Exception e) {
if(exceptionFlag) e.printStackTrace();
}
return "An Error has occured";
}
... and then in the calling code, something like this should be done ...
string result = foo();
if (result.Contains("Error"))
{
Response.Write("An Error has occured");
Response.Redirect("Error.aspx?msg=An+Error+has+occured");
}


 

Re: You Can Never Be Too Cautious

2005-01-20 16:20 • by Blue
28235 in reply to 28233

I have to assume the preceding post is a joke.


Please, tell me it is!

Re: You Can Never Be Too Cautious

2005-01-20 16:24 • by skicow
28236 in reply to 28233
:

WTF.  Doesn't this guy know exceptions are bad for performance?

public string foo()
{
boolean
exceptionFlag = true;
try {
initializeResources();
compareAggregates();
exceptionFlag = false;
} catch(Exception e) {
if(exceptionFlag) e.printStackTrace();
}
return "An Error has occured";
}
... and then in the calling code, something like this should be done ...
string result = foo();
if (result.Contains("Error"))
{
Response.Write("An Error has occured");
Response.Redirect("Error.aspx?msg=An+Error+has+occured");
}


 


Trapping errors by parsing the return string just rubs me the wrong way...[*-)]


Plus this will only work if the method a string type....we can't tell from this code if it is or not.

Re: You Can Never Be Too Cautious

2005-01-20 16:31 • by brandonh6k
28237 in reply to 28232
:

His coworker must be an old VB 4/5/6 developer... what's they've done is create the Java equivalent of "On Error Goto" the end of the try catch block


If the ExceptionFlag is set the false, then no stacktrace will ever be printed, and no "An Error has occured" exception will ever be thrown. it will simply "resume next" at the end of the try catch block. Wink



Wow.  I didn't think you could actually get On Error Resume Next behavior in C#.  That's amazing...  amazingly WTF-ish.

Re: You Can Never Be Too Cautious

2005-01-20 16:44 • by

Actually, this is pretty solid code.


I tried it using basic HTML and a tin can and it worked wonders.


It's amazing what you can do with magic.

Re: You Can Never Be Too Cautious

2005-01-20 16:58 • by

this was java, not C#


(C# uses bool not boolean)


 


us C# programmers know better! :P

Re: You Can Never Be Too Cautious

2005-01-20 17:17 • by Razzie

Scott,


That example was pretty obvious. I was referring more to what Anonymous said, in that not everything that is throwed will be catched.


So it seems Java and C# differ from this. In Java you can throw every object that is a subclass of Throwable. In C#, everything that can be throwed is a subclass of Exception or the Exception object itself, thus in C#, that code would catch everything. I didn't know this about Java before.

Re: You Can Never Be Too Cautious

2005-01-20 17:50 • by Katja
28244 in reply to 28212

In Delphi there's something similar. Basically, in Delphi you can raise (throw) any kind of object as if it's an exception. Thus something like:


  raise TComponent.Create(nil);


will also raise an exception, but one that isn't a standard exception type. You can have quite a lot of confusing fun with that... [;)]

Re: You Can Never Be Too Cautious

2005-01-20 17:52 • by

Katja...you are too young to know about the wonders of Delphi!

Re: You Can Never Be Too Cautious

2005-01-20 18:15 • by Razzie
28248 in reply to 28245

Well I for one didn't know they teach Delphi in Holland... at least I haven't seen it yet ;) Apparently, I was wrong!


But anyway, do you really have to be old and wrinkled to really really know the secrets and fun of Delphi? Poor Katja ;)

Re: You Can Never Be Too Cautious

2005-01-20 18:33 • by Katja
28252 in reply to 28248
Razzie:

Well I for one didn't know they teach Delphi in Holland... at least I haven't seen it yet ;) Apparently, I was wrong!


But anyway, do you really have to be old and wrinkled to really really know the secrets and fun of Delphi? Poor Katja ;)



Actually, my dad works as a Delphi developer for some software company and he has quite a collection of books too. When I started to get interested in programming about 7 years ago, he decided to teach me some things himself. But you're right. During my studies they're teaching me a bit of all kinds of languages. A bit of C++, a bit of Java, a bit of .NET but no Delphi. No problem for me, though... I know enough about Delphi already. [H]


And believe me, young people pick up computer skills a lot faster than you old guys... [:P]

Re: You Can Never Be Too Cautious

2005-01-20 19:26 • by Maurits
28253 in reply to 28252
Bad: Response.Redirect("Error.aspx?msg=An+Error+has+occured");

Why? Opens Error.aspx to an HTML injection attack.
Consider someone linking to

Error.aspx?msg=Hi+we+suck+you+should+go+to+competitor.example.com

Re: You Can Never Be Too Cautious

2005-01-20 19:57 • by Bellinghman
28255 in reply to 28245
:
Katja...you are too young to know about the wonders of Delphi!


Hey, Delphi is still going - the current version of the IDE includes C# as well.

Re: You Can Never Be Too Cautious

2005-01-20 20:25 • by
It looks to me as if the guy is confused by the try/catch structure... he's put the exceptionFlag in there so he doesn't get caught like forgetting the 'break' in a C switch statement.

Re: You Can Never Be Too Cautious

2005-01-20 20:48 • by Free
28257 in reply to 28233
:

WTF.  Doesn't this guy know exceptions are bad for performance?

public string foo()
{
boolean
exceptionFlag = true;
try {
initializeResources();
compareAggregates();
exceptionFlag = false;
} catch(Exception e) {
if(exceptionFlag) e.printStackTrace();
}
return "An Error has occured";
}
... and then in the calling code, something like this should be done ...
string result = foo();
if (result.Contains("Error"))
{
Response.Write("An Error has occured");
Response.Redirect("Error.aspx?msg=An+Error+has+occured");
}


 


Must be a Joke, because the cost is from throwing up an exception, not from swallowing it.


I had to implement an webservice from a spec. They threw away my Data Transfer Object prototype because they could use DataSets. All I wanted was a list of properties so they sent it out to a consultant. If there is an exception, it is caught, a dummy row with an "error" column containing the Exception message is returned.


Rather than just parsing the return value for "Error", you must check that datatable for the presence of a column called "error".


 


 

Re: You Can Never Be Too Cautious

2005-01-21 00:19 • by
28258 in reply to 28210
:
Actually, catch(Exception e) does not catch everything that can be thrown...







Actually, yes it will... to be more precise, it will catch
any exception that should be caught and the only thing it won't catch is an
Error which is the only other class that extends Throwable.



To quote the JDK javadocs:

" An Error is a
subclass of Throwable that
indicates serious problems that a reasonable application should not try to
catch."



In other words an Error is something much more serious than an exception and
usually indicates a problem with the VM itself (RTFM).



So, if the code your writing throws Errors (particularly if they are then
caught) I think we can safely say we've got another WTF on our hands.



BTW - My favourite is the CoderMalfunctionError (Yes I know its out of context,
but it just seems to fit here).





Re: You Can Never Be Too Cautious

2005-01-21 00:22 • by ProffK
28259 in reply to 28244
I remember some C++ (which is not my forte) code I worked on last
year.  They even threw an integer: the COM error code.  That
had me going for a while.

Re: You Can Never Be Too Cautious

2005-01-21 00:24 • by
28260 in reply to 28233
:

WTF.  Doesn't this guy know exceptions are bad for performance?

public string foo()
[...]
... and then in the calling code, something like this should be done ...
string result = foo();
if (result.Contains("Error"))
{
Response.Write("An Error has occured");
Response.Redirect("Error.aspx?msg=An+Error+has+occured");
}



Your joking right? I think?

Re: You Can Never Be Too Cautious

2005-01-21 00:28 • by
28261 in reply to 28240
:

this was java, not C#


(C# uses bool not boolean)



us C# programmers know better! :P





Oh, so thats what that IO operation
"Response.Redirect("YourMomIsHot.html");" that doesn't throw a useful
IOException is all about... good thing you C# folks know better!



Us Java guys are just going to have to deal with the exception if the application can't write to the stream... poor us...



Re: You Can Never Be Too Cautious

2005-01-21 00:50 • by
Do they have a Delphi.net implementation? I can't wait.

Re: You Can Never Be Too Cautious

2005-01-21 02:58 • by Razzie
28264 in reply to 28252

Katja:


And believe me, young people pick up computer skills a lot faster than you old guys... Stick out tongue 


Hey... leave me out of that ok? Just about to graduate this year myself ;)

Re: You Can Never Be Too Cautious

2005-01-21 03:09 • by
28265 in reply to 28258
:







  wrote:




Actually, catch(Exception e) does not catch everything that can be thrown...



Actually, yes it will... to be more precise, it will catch any exception that should be caught and the only thing it won't catch is an Error which is the only other class that extends Throwable.


Is there something that prevents writing own Throwables that are neither Errors nor Exceptions?


About the code... I think the biggest wtf there is catching an exception and then instantly throwing a generic "an error occurred"-exception which effectively hides all valuable information about the original exception. The next catch-block will get only the generic exception.

Re: You Can Never Be Too Cautious

2005-01-21 04:21 • by ProffK
28269 in reply to 28264
Leave me out as well, I'm 35 but I pick up damn quickly. 

Re: You Can Never Be Too Cautious

2005-01-21 04:22 • by
28270 in reply to 28236
Wouldnt you just edit web.config to turn custom errror page on instead

Re: You Can Never Be Too Cautious

2005-01-21 05:39 • by Bellinghman
28272 in reply to 28262
:
Do they have a Delphi.net implementation? I can't wait.


I think Delphi.net came out a few years ago. And that C# Builder came out two or three years ago - Borland now seem to have merged them.

(Well, once you start targetting CLR underneath, much of the code generation becomes common to both. And C# and Delphi are pretty similar languages in a lot of ways, both being Anders Hejlsberg's babies.)

Re: You Can Never Be Too Cautious

2005-01-21 07:24 • by im_not_jose
me thinks that this may also be attributed to lazy/sloppy programming, and not necessarily a WTF scenario.



perhaps   initializeResources();   
or   compareAggregates(); used to return a bollean value,
based on whether they ''performed'' or not... and the
''if(exception)'  stuff used to just come afterwards... then the
programmer discovered try {} catch {} statements and quickly cut &
pasted the code around...?



hm, probably not. occam's razor would argue the wtf scenario far more likely.



yeah, delphi is still alive and well ( http://dotnet.borland.com/ ).
tis a shame no one uses it really nowadays (or am i wrong?), except for
shareware-typish software...? katja, i envy you. :) i always wanted my
dad to be a soft developer/geek[8-|] who wanted to teach me such stuff
as a kid. you have no idea how much faster and easier it is to learn
everything that way. nah, instead my father was into business
management type stuff... the kind of things I am least interested and
worst at...[8-)]



Re: You Can Never Be Too Cautious

2005-01-21 08:19 • by
28275 in reply to 28237
so yet again the real problem is VB [:D]



Re: You Can Never Be Too Cautious

2005-01-21 09:02 • by znaps
28277 in reply to 28258
:
  wrote:
Actually, catch(Exception e) does not catch everything that can be thrown...







Actually, yes it will... to be more precise, it will catch
any exception that should be caught and the only thing it won't catch is an
Error which is the only other class that extends Throwable.



To quote the JDK javadocs:

" An Error is a
subclass of Throwable that
indicates serious problems that a reasonable application should not try to
catch."



In other words an Error is something much more serious than an exception and
usually indicates a problem with the VM itself (RTFM).



So, if the code your writing throws Errors (particularly if they are then
caught) I think we can safely say we've got another WTF on our hands.



BTW - My favourite is the CoderMalfunctionError (Yes I know its out of context,
but it just seems to fit here).





Re: You Can Never Be Too Cautious

2005-01-21 09:03 • by znaps
28278 in reply to 28277
Oops, my text didn't get included there.



I was saying the quoted text was wrong, because I can create my own subclasses of Throwable, which are not Errors.



It may not be an everyday thing to do, but it has its uses.

Re: You Can Never Be Too Cautious

2005-01-21 09:12 • by Katja
28279 in reply to 28278
znaps, when would it have it's uses then? I know that in Delphi it
could be very funny to raise just a component instead of an exception
because most exception handling clauses would not expect to receive a
non-exception object and thus they will puke out another exception.
Great if you want to bring the application down from your simple piece
of code, but what other uses would it have?

Re: You Can Never Be Too Cautious

2005-01-21 10:29 • by
28280 in reply to 28272

Bellinghman:
I think Delphi.net came out a few years ago.


Are you serious? What's next, PASCAL.NET?


Eww.

Re: You Can Never Be Too Cautious

2005-01-21 10:39 • by Katja
28281 in reply to 28280
Ehm, Delphi is Pascal, thus Delphi.NET IS Pascal.net...



And there are several other languages converted to .NET too. I even
heard of an ASM.NET which you can use to write .NET applications in,
that's right, Assembler code...[:$]

Re: You Can Never Be Too Cautious

2005-01-21 12:49 • by Bellinghman
28287 in reply to 28280
:
What's next, PASCAL.NET


Hey, don't knock it. After all, there's VB.NET, and the original VB was even less appropriate as a modern OO language.

The great attraction of Delphi out there, so far as I could see, was that it was almost as easy to get into as VB, but much better for medium to large projects.

Not that a poor Delphi programmer couldn't screw up.

Not that there aren't good VB programmers who could build decent large projects using that language.

Oh, and Delphi 5 came with a much better set of controls out of the box.

Delphi was, effectively, a better VB, just using Pascal syntax rather than BASIC.

(Disclaimer - I am not, nor have I ever been, a Delphi programmer.)

Re: You Can Never Be Too Cautious

2005-01-21 15:45 • by znaps
28313 in reply to 28279
Katja:
znaps, when would it have it's uses then? I know that in Delphi it
could be very funny to raise just a component instead of an exception
because most exception handling clauses would not expect to receive a
non-exception object and thus they will puke out another exception.
Great if you want to bring the application down from your simple piece
of code, but what other uses would it have?





Say I wrote my own VM and wanted to define some new types of
"exceptional" happenings. Then I might subclass Throwable (or error).
Obviously since I created these new objects, I'll be catching them
somewhere or requiring client code to catch them.

Re: You Can Never Be Too Cautious

2005-01-21 20:14 • by andyandy
28331 in reply to 28281

Katja:
Ehm, Delphi is Pascal, thus Delphi.NET IS Pascal.net...

And there are several other languages converted to .NET too. I even heard of an ASM.NET which you can use to write .NET applications in, that's right, Assembler code...Embarrassed


You're thinking of this "ASP.NET: ASM to IL compiler"? Read about it some months ago, but haven't used it yet. Maybe I will use it for the next ASP.NET project :)


List of (most?) .NET languages. Note that there are four Pascal variants listed.

Re: You Can Never Be Too Cautious

2005-01-22 15:08 • by
28335 in reply to 28212
Exception extends Throwable in Java (is this Java?)... so to truly catch every problem, you'd



try {

} catch (Throwable e) {

}



I had the joy of working with a student who threw that beautiful block around every Exception-throwing method in the project [8-)]

« PrevPage 1 | Page 2Next »

Add Comment