- 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
You forgot Thread.sleep(...) in the catch block. That's much less wtf-y.
captcha: abigo. "I have a head code, abigo."
Admin
This. I have to help maintain a ludicrously complicated iOS application. The original authors were .NET devs and decided that the .NET exception model should work just fine in Objective-C.
Now we have tons of user-defined NSException subclasses that can boil up from the bowels of the networking layer to be used for logic flow control in the UI. Good times.
Admin
By, George, you're right! That does make it much less wtf-y! With a sleep(), this WTF will waste much less CPU time while looping infinitely.
Thanks.
Admin
If I had to chose, I'd much rather have it fail in seconds whenever there's an error than have it pass when there's a time-sensitive error and fail in minutes for any other kind.
Admin
This TDWTF really made me chuckle. That and "ben's" comments and those who responded to him.
Admin
This is not a spam Akismet :)
What does it take to post a url! How I Learned About Exception Handling
ahhh....
Admin
Hey yelling works :)
Admin
Then be a dear and leave a comment as to why you're leaving an empty catch block. And be damn sure you're only catching that exact exeption. Which means in the catch block you should be testing the message in the exception to be sure it's a 404 error and not one of the many other possible HTTPExceptions. If it's not the exception you're looking for, then the exception should move along up the stack.
Admin
Then I suppose it depends on your experience. If you want to have error handling mechanisms being called from all over your code. But the idea behind the exception mechanism seems to be to allow you to have one error handling routine and have it act automatically when an unexpected error occurs. (In a similar idea to security being implemented as a layer on top of the main functionality of the program.)
EvilSnack, you make an interesting point, but it also sounds like you simply haven't had much experience with exceptions to be able to make an informed judgment.
Also, for a properly defined function, the return value should only be of a specific type. This means that if you have error codes in it, then there are some return values that you cannot actually return or everything that may read the return value has to know how to parse the return value for errors. Exception handling prevents you from having to overload your returned type with other meanings.
Admin
ISTM that you are stuck with those groupings. If all errors come to the same spot, you can group as you wish.
I generally have a global error handler that does minimal cleanup (closing of files, etc.), logs an error, and ends the app.
Where I can deal with specific errors, I set up an error handler, perform the operation that could throw the error, restore the global error handler, then ...
I check through the error codes that can be handled. If the error code is one of those, the corresponding error handling is done. If it is not one of them, there is nothing I can do about so I rethrow the error.
I remember using an encryption library written in Java. It threw lots of things that I had no idea what they were. I had to trap the each of them. I suppose I could have trapped them all in a general catch block, but wouldn't I lose what the error was?
I find try-catch to be very awkward.
Sincerely,
Gene Wirchenko
Admin
Switching back and forth between .NET and PHP, I would just like to say that exceptions are approximately ONE_BILLION times better than checking for errors.
Admin
If at first you don't succeed ...
Admin
The best part about this is that the inevitable StackOverflowException will be caught, and the recursion will be resumed, causing more SOE even if the getPersonsInformation() call would magically have succeeded on a subsequent call.
Admin
Admin
Admin
else {throw;} - not else {throw hex;}
Admin
Six of one.
Admin
Why not add a little recursive multi-threading to the mix:
public Collection getPersonsInformation(String department,String function) throws LookupException { ServiceLocator locator = ServiceLocator.getInstance(); LookupInterface lookup = locator.getLookupLocal();
Admin
OK, surely I am missing something. The arity of the calls are different. The first has 3 arguments, the second 2. I am not a Java wrangler but I know it has method overloading. I assume the method that takes 2 arguments returns some default collection not dependent on LoginInfo. The one that takes 3 uses some global LoginInfo that is more specific. I'm guessing LoginInfo has global scope. The RWTF is the global?
Admin
Sure, if you don't care about the whole stack trace and instead want to change where the error came from.
Admin
Admin
But even when the toString() method of the Exception throws away information, you could always call t.getClass().getName(), t.getMessage() / t.getLocalizedMessage() , or whatever else the API provides.
Admin
IMHO, I think that developers have become too dependent on the stack trace for exceptions.
Admin
(Ad hominem snipped.)
...but at the expense of more work to achieve the same result. To be useful the function need only return one result indicating error, with the specifics available by some other means (global variable, system call, etc.). Any other return represents valid data. It's not really more work than an exception, especially if you do it right.Admin
I agree. There seems to be a tendency to use exceptions just because they're available. This code should at least die informatively on fatal errors.
Admin
It's StackOverflowError, so it wouldn't be caught. and that's the reason why catching Throwable is usually a bad idea.
Admin
FTFY.
Admin
If at frist you don't succeed, try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try try
Admin
I guess I should have kept on reading past Mat's first post...
Admin
Ironically, I don't think the programmer found this gem of code on stackoverflow.com.
Admin
That's the same reason Python has resisted implementing tail-recursion too. It's a nice feature for certain cases, but it makes exception reporting vastly more difficult - and oddly enough, most people thing functioning error handling is more important than the rarely-needed ability to implement recursive algorithms...
Admin
shouldn't the obligitory xkcd link have been obliged by now?
Admin
Heh... about 6 months ago, I was trying to track down the cause of a StackOverflowError in some third-party code I was using.
Unfortunately, it's now utterly impossible to search for that error... that otherwise useful website just drowns out everything - and you can't easily exclude it from results, because that's where you'd expect to find them...
Admin
This is the problem with all this new-fangled OO stuff....the more you simplify things, the more simpletons will use it. Unfortunately, I think kids of today don't understand things like exception handling, and are taught to keep things simple - and so why bother catching multiple exceptions explicitly when the language allows you just to catch a generic one?
Also, get off me goddamn lawn!
Admin
Admin
Admin
I can sort of imagine advantages to an exception based approach, but I can also think of advantages to an error-code based approach (and let's remember not all languages are OO....).
Then again, when all you have is a hammer.....
Admin
Admin
No, the method signature in the catch block is different. The method called in the catch block has one argument. The overall method has two arguments. For all we know, the method being called does not recurse.
Admin
Admin
Exceptional trampolining using anonymous classes? Eww. Java sucks!
Admin
Admin
Admin
Errors will happen, no matter how much you minimise the likelihood, and when they do, the language needs to make it easy to find them. And while tail-recursion is a useful feature, it's not going to make any great difference in reducing the frequency of errors.
Admin
Admin
Admin
actually the StackOverflow in java is an Error; though an error is a throwable, it is not an exception, and would therefore not be caught. ...and it would be difficult to handle that one anyhow, but different discussion
Admin
To keep from losing the error information, "convert" the exception to a RuntimeException (those don't have to be declared in the method header). Example:
The last "catch (Exception e)" catches any type of Exception you didn't already catch. Then that Exception is passed into a new RuntimeException and thrown again.
When the program fails (from the thrown RuntimeException) you still get the full information set, which will read something like, "Here are the details of your RuntimeException, which occurred (here) and was caused by another (Exception e) which occurred (there)." The complete report contains everything you need to know.
There are a couple of other ways you could save the information, but this is best: you might want to know where to add another catch, for example.
Admin
I forgot to mention that that particular library (actually, it's pywikipedia) have its own exception class for page not found error (pywikipedia.NoPage), so I just catch NoPage exception and ignore it :)
Admin
That's like what one lungfish says to another lungfish: "IMHO, I think that land-dwellers have become too dependent upon legs for locomotion."