Posted by
Daniel Hornung on
URL: http://imagej.273.s1.nabble.com/Return-On-Error-tp3690523p3690527.html
Michael Doube wrote:
> I like the idea but don't see it very often in other people's code.
>
> Perhaps an example would help to illustrate the technique, especially
> for those of us relatively new to Java, and who want to improve our skills?
>
> Michael
Real-life examples tend to be rather lengthy, but if you want to learn about
exceptions, this would be one possible starting point:
http://java.sun.com/docs/books/tutorial/essential/exceptions/index.htmlI'll try a short example nonetheless:
public void run(ImageProcessor iProc)
{
try{
doTheRealStuff(iProc);
} catch (MyEndpluginException e) {
// oops, an exception was thrown,
// so terminate the plugin now
return;
}
}
protected void doTheRealStuff(ImageProcessor iProc)
throws MyEndpluginException
{
// do the calculations here
//...
// unrecoverable problems found? throw an exception!
if(problemSearching()) {
throw new MyEndpluginException();
}
// no problem found? simply continue
// ...
someOtherMethod(); // if this one throws an exception, it is
// simply passed "upwards"
}
protected void someOtherMethod()
throws MyEndpluginException
{
// do more stuff here, including maybe throwing an exception
}
I hope this gives you a basic understanding,
Daniel
--
Daniel Hornung
Biomedical Physics Group
Max-Planck-Institute for Dynamics and Self-Organization
Bunsenstr. 10
D-37073 Goettingen
(+49) 551 5176 368