- Feature Articles
- CodeSOD
- Error'd
- Forums
-
Other Articles
- Random Article
- Other Series
- Alex's Soapbox
- Announcements
- Best of…
- Best of Email
- Best of the Sidebar
- Bring Your Own Code
- Coded Smorgasbord
- Mandatory Fun Day
- Off Topic
- Representative Line
- News Roundup
- Editor's Soapbox
- Software on the Rocks
- Souvenir Potpourri
- Sponsor Post
- Tales from the Interview
- The Daily WTF: Live
- Virtudyne
Admin
Your users send you a screen shot? Could that be a clue you're doing it wrong?
Admin
Ok, one more try... ;)
Admin
This is why exceptions were invented. So you don't have to create a fancy data type just to handle error messages in your program. They actually work pretty good if you use them right. The fact that exceptions are often badly used is not the fault of the construct it's self.
Admin
Maybe something like:
What, you don't like Outlook as an Error Log? :)
Exactly.
Admin
No there are not, but that is ok, because the exception of no more exceptions will be caught.
Admin
Close, but here's a small refinement...
Admin
Exceptions are basically a way to circumvent static type checking.
A function that declares "returns Cake" states that it only ever will return Cake (or null, if you are using one of those billion-dollar-mistake-programming-languages, which Haskell is not part of. Yes you read that right: No null in Haskell!).
However in programming languages with Exceptions, the function may actually return a Cake, or throw an Exception.
The "returns Cake" is a lie!
Which Exception may it return? Well C++ allows even to throw any Object, so the return type of your program has become "returns Mail or any Object".
And when an Exception is thrown, it is unclear where it will be processed. It might be not processed at all, just ending the program.
So you lose static type safety and any predictability with Exceptions.
The Either-Monad, as shown in two other posts before, does not only exist in the functional programming language Haskell and the functional-OO-hybrid Scala (both statically type-checked), but can be also implemented in regular Java.
(You want that Either-Type only have two subclasses: Left and Right. You can do this by giving Either a private constructor and put the Left- and Right-implementations in the same file as Either).
However the absence of Lambdas in pre-JDK1.8-Java makes it more awkward to use.
Admin
I think the try/catch is needed because askking for the inner exception when you've already reached the innermost one returns a null, so when you try to go yet another level you throw a null-pointer exception. So they have to catch that and discard it. This is a programming technique called "lazy and lame".
Admin
"Each exception thrown by our application must be caught handled before the user sees it." Except then it pops up a message box to display the error, which, I would think, means that "the user sees it".
Well, I suppose you did catch the message before the user sees it. You caught the message, and then promptly displayed it for the user. Thus you caught it before the user saw it. Requirement met. I've got to remember that technique. And remember to question what the word "is" means.
captcha: appellatio. I'm not sure what it means but I think it's something obscene.
Admin
Exception.ToString() perhaps?
Admin
Nice one :D
Admin
This. If I had a cent for every "developer" printing/logging ex.Message, and not printing ex.ToString() anywhere, I would be filthy rich.
And don't get me started with the "that is too ugly for my users, just output ex.Message!!!" folks. Hopeless. Idiots. </rant>
Admin
Beat me to it :)
Admin
I disagree. If the function executes without an exception, the caller then moves on to the next statement. If there is an exception, that propagates up the stack, and the caller has to handle it, or it continues going up the stack.
Saying that a function "returns an exception" is inaccurate. It may generate one, but not return one.
However, I do agree with what you said about where an exception is processed. It's just part of the scope of the code. It keeps going up until it is processed. Whether it's by the app that generated the exception, or by the environment that it's running in, something went wrong, and needs to be addressed somewhere.
Admin
It sounds like you're looking for Haskell.
Admin
This. Also, how do you lose predictability? In any exception-having language I've seen, every single possible exception at any point has a very predictable path. Either it's caught or it's sent up the stack.
Admin
Are glaring grammar errors in the featured articles a meme?
Admin
I believe TRWTF is actually that you would probably be better off just using Exception.GetBaseException() to find the original exception.
Admin
Admin
Half the battle in .NET is knowing the framework and what's already done for you.
Admin
Inner piece ... or try to reference a null and die a horrible death like we are trying to avoid by having a generic handler in the first place. Nice.
Admin
Please append the error strings and show only one box.
Admin
It is often non-obvious when a function will throw an exception, particularly in c# which has no checked exceptions, and particularly if the function you are calling calls an exception throwing function without a try catch. Furthermore, not ending the program can be nice in some cases, but in others it makes debugging tricky. Your program just got into a funny state, and you lost the part of the stack that caused to problem.
Admin
Yes, their all idiots for not having complete knowledge the .net API. Everyone should know this, even if they are java or c++ programmers.
Admin
You don't need to call both Contains and Add on the HashSet; Add will return false if the set already contains the value you're trying to add.
You also don't need to maintain a separate count of the items you've visited.
And anyone who writes "throw new Exception" should be boiled in a vat of elephant dung; ALWAYS use a more specific exception type!
Admin
All of which goes to show that writing good and correct code, even for seemingly simple tasks like catching an Exception, is almost always much, much more difficult than anyone initially realizes.
Admin
Well... I guess we now know that an Inception is the deepest INnerexCEPTION posible.
It all starts with a well placed NullPointerException.
Tired from luptatum with all these exceptions.
Admin
yay for multiple return values.
http://golang.org/doc/articles/error_handling.html
Admin
The original code lends itself to a recursive implementation:
It will usually finish with a null dereference exception unless you throw one of those weird exceptions from earlier comments in which case you'll get a stack overflow or some such.Admin
So I heard you like Exceptions...
Admin
The exception handling code is a major performance bottleneck? I think something went wrong there.
Admin
if(!FooA()) goto Error if(!FooB()) goto Error if(!FooC()) goto Error
return; :Error HandleError();
or this: if(FooA()) if(FooB()) if(FooC()) return;
HandleError();
than to read code like this: try{ FooA(); FooB(); FooC(); } catch(Exception e){ HandleError(e); }
Not to mention code performs so much better when you're constantly checking for error states. </sarcasm>
Admin
this is sooooo deep.
Admin
For C++, you could use boost::variant http://www.boost.org/doc/libs/1_52_0/doc/html/variant.html. With destructors, you could even require errors to be "defused" to avoid crashing the program. :P
For Haskell, not only is the Maybe typeclass a possible solution https://en.wikibooks.org/wiki/Haskell/Hierarchical_libraries/Maybe, it's the preferred one.
More strongly-typed languages should allow this.