Re: newbie: exception handling (and Eclipse)
Don't mess around with the architecture of your application just to use the same exception handling code. If there is no functional relation or dependency between method1 and method2, you should not be calling method2 inside method1. If you find you want to use the same code to handle the exceptions from three different methods, you can either the method calls inside a single try block with the common exception handling in the following catch block, or give each method its own try/catch and put the exception handling code into a separate method which you call from the individual catch blocks, e.g:
Code:
void outerMethod() {
...
try {
...
method1();
...
method2();
...
method3();
...
}
catch (FooException fe) {
... // handle Foo exceptions from the methods
}
catch (BarException be) {
... // handle Bar exceptions from the methods
}
}
// alternatively
void method1() {
try {
... // do method1 stuff
}
catch (FooException fe) {
handleFooException(fe); // call common handler
}
catch (BarException be) {
handleBarException(be); // call common handler
}
}
...
void handleFooException(FooException fe) {
... // common Foo exception handling code
}
... // etc.
However, in my experience, it's not often that the second technique (using common exception handler methods) is useful or necessary, because some variation on the first technique is usually sufficient.
The most important single aspect of software development is to be clear about what you are trying to build...
B. Stroustrup
Re: newbie: exception handling (and Eclipse)
There is good pictorial and text explanation of exception handling in Java at
http://bharatchhajer.blogspot.com/20...g-in-java.html . Its a good and complete overview of different situations and how to handle them.
Re: newbie: exception handling (and Eclipse)
Advertising your own blog site especially by passing it off as a good site you've found is not acceptable here ie by saying "There is good pictorial and text explanation of exception handling in Java at". If you have written a tutorial that might help the OP then say something like "I've written a tutorial on XXX that might help, you can find it at".
I've read your tutorial and ignoring the usual smattering of typos that all of us make when posting stuff, I didn't find it particularly easy to understand. Your comparison to a car journey isn't well explained and is not used consistently throughout the article. And finally, I disagree with your comment "Unchecked exceptions should never if handled, so the compiler does not force you to decide on its handling." Assuming you meant to say "should never be handled", this is wrong. Generally speaking unchecked exceptions are not handled but there are cases when when it is useful to handle them such as catching a NumberFormatException when parsing external data and converting it to a number.