|
-
July 6th, 2010, 05:56 AM
#4
Re: newbie: exception handling (and Eclipse)
What kind of Reader has next() and hasNext() methods? I know ResultSet, Scanner, and Iterator have next() methods, but no Readers.
It doesn't matter whether you've called hasNext() before next(), if the next() method is declared to throw exceptions, then you need to decide how to handle them. If it isn't, you don't need to.
[Note that in the case of Scanner, for example, the next() method isn't declared to throw exceptions - so you don't have to write code to deal with them. However, the API docs say that it can throw certain exceptions in some circumstances. These are RuntimeException subtypes, known as 'unchecked' exceptions because you're not obliged to handle them, but if you don't, and the exceptions are thrown, the application will terminate. Generally, you'd only put in code to catch and handle these exceptions if there's a possibility that your code might trigger them - e.g. multi-threaded code where perhaps the Scanner is being accessed by more than one process.]
You should put your catch code blocks at the closest point where you can handle the exception and do something useful, such as retrying the operation, or tidying up and abandoning the operation. Sometimes this will be in the same method that throws the exception, sometimes in the method that calls that method, and sometimes it will be the main method. Usually, by the time it reaches the main method, there's nothing to do but output an informative error message and quit.
As soon as we started programming, we found out to our surprise that it wasn't as easy to get programs right as we had thought. Debugging had to be discovered. I can remember the exact instant when I realized that a large part of my life from then on was going to be spent in finding mistakes in my own programs...
M. Wilkes
Please use [CODE]...your code here...[/CODE] tags when posting code. If you get an error, please post the full error message and stack trace, if present.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|