Return On Error

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

Return On Error

David Webster
Is there any way to return to ImageJ from a plugin when a potential error
is detected (e.g. if n input parameter has a bad value.) System.exit()
kicks me all the way out of ImageJ.

David Webster
Reply | Threaded
Open this post in threaded view
|

Re: Return On Error

Michael Doube
Hi David

In a PlugIn the run() method returns void so you can just

return;

If one of the other methods that run() calls finds an error it could
return an error value to run(), which run() then uses to decide whether
or not to return, or do something else.

A common use is to call a showDialog() method in the run() method, which
shows a dialog for user input and returns false if the dialog is
cancelled. When the dialog method returns false then run() returns and
the plugin terminates.

public void run(String arg){
        if (!showDialog()){
           return;
        }

//otherwise keep executing run()

}

If you hit OK, showDialog() returns true and the plugin continues.

Michael

David William Webster wrote:
> Is there any way to return to ImageJ from a plugin when a potential error
> is detected (e.g. if n input parameter has a bad value.) System.exit()
> kicks me all the way out of ImageJ.
>
> David Webster
Reply | Threaded
Open this post in threaded view
|

Antwort: Re: Return On Error

Joachim Wesner
Hmm,

IMHO this seems not very Java-like as it also would require to augment all
calls to lower
level routines with error checking, just what one would like to avoid (Or
did I get something wrong?)

Wouldn´t it be better to use to use a user-defined exception to throw at
the intended point
of premature return and use try-catch in the run method (which
unfortunatelly also would not leave the
remaining source code totally unchanged, as one would need to declare all
intermediate routines
as "throws xxx", which however would only be required at the top of each
method, not on each call..

Just my ideas...

I hhink some time ago we already had this topic somewhere in this forum!?

Joachim




                                                                           
             Michael Doube                                                
             <m.doube@IMPERIAL                                            
             .AC.UK>                                                    An
             Gesendet von:              [hidden email]                
             ImageJ Interest                                         Kopie
             Group                                                        
             <[hidden email].                                       Thema
             GOV>                       Re: Return On Error                
                                                                           
                                                                           
             06.11.2009 21:11                                              
                                                                           
                                                                           
              Bitte antworten                                              
                    an                                                    
              ImageJ Interest                                              
                   Group                                                  
             <[hidden email].                                            
                   GOV>                                                    
                                                                           
                                                                           




Hi David

In a PlugIn the run() method returns void so you can just

return;

If one of the other methods that run() calls finds an error it could
return an error value to run(), which run() then uses to decide whether
or not to return, or do something else.

A common use is to call a showDialog() method in the run() method, which
shows a dialog for user input and returns false if the dialog is
cancelled. When the dialog method returns false then run() returns and
the plugin terminates.

public void run(String arg){
             if (!showDialog()){
                return;
             }

//otherwise keep executing run()

}

If you hit OK, showDialog() returns true and the plugin continues.

Michael

David William Webster wrote:
> Is there any way to return to ImageJ from a plugin when a potential error

> is detected (e.g. if n input parameter has a bad value.) System.exit()
> kicks me all the way out of ImageJ.
>
> David Webster



______________________________________________________________________
This email has been scanned by the MessageLabs Email Security System.
For more information please visit http://www.messagelabs.com/email 
______________________________________________________________________
Reply | Threaded
Open this post in threaded view
|

Re: Antwort: Re: Return On Error

David Webster
All,

I'm not sure about the non "Java-ness" issue, but like the suggestion
about throwing an exception and using a try/catch.

David Webster
Reply | Threaded
Open this post in threaded view
|

Re: Antwort: Re: Return On Error

Michael Doube
In reply to this post by Joachim Wesner
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

Joachim Wesner wrote:

> Hmm,
>
> IMHO this seems not very Java-like as it also would require to augment all
> calls to lower
> level routines with error checking, just what one would like to avoid (Or
> did I get something wrong?)
>
> Wouldn´t it be better to use to use a user-defined exception to throw at
> the intended point
> of premature return and use try-catch in the run method (which
> unfortunatelly also would not leave the
> remaining source code totally unchanged, as one would need to declare all
> intermediate routines
> as "throws xxx", which however would only be required at the top of each
> method, not on each call..
>
> Just my ideas...
>
> I hhink some time ago we already had this topic somewhere in this forum!?
>
> Joachim
>
>
>
>
>                                                                            
>              Michael Doube                                                
>              <m.doube@IMPERIAL                                            
>              .AC.UK>                                                    An
>              Gesendet von:              [hidden email]                
>              ImageJ Interest                                         Kopie
>              Group                                                        
>              <[hidden email].                                       Thema
>              GOV>                       Re: Return On Error                
>                                                                            
>                                                                            
>              06.11.2009 21:11                                              
>                                                                            
>                                                                            
>               Bitte antworten                                              
>                     an                                                    
>               ImageJ Interest                                              
>                    Group                                                  
>              <[hidden email].                                            
>                    GOV>                                                    
>                                                                            
>                                                                            
>
>
>
>
> Hi David
>
> In a PlugIn the run() method returns void so you can just
>
> return;
>
> If one of the other methods that run() calls finds an error it could
> return an error value to run(), which run() then uses to decide whether
> or not to return, or do something else.
>
> A common use is to call a showDialog() method in the run() method, which
> shows a dialog for user input and returns false if the dialog is
> cancelled. When the dialog method returns false then run() returns and
> the plugin terminates.
>
> public void run(String arg){
>              if (!showDialog()){
>                 return;
>              }
>
> //otherwise keep executing run()
>
> }
>
> If you hit OK, showDialog() returns true and the plugin continues.
>
> Michael
>
> David William Webster wrote:
>> Is there any way to return to ImageJ from a plugin when a potential error
>
>> is detected (e.g. if n input parameter has a bad value.) System.exit()
>> kicks me all the way out of ImageJ.
>>
>> David Webster
>
>
>
> ______________________________________________________________________
> This email has been scanned by the MessageLabs Email Security System.
> For more information please visit http://www.messagelabs.com/email 
> ______________________________________________________________________

--
Dr Michael Doube  BPhil BVSc PhD MRCVS
Research Associate
Department of Bioengineering
Imperial College London
South Kensington Campus
London  SW7 2AZ
United Kingdom
Reply | Threaded
Open this post in threaded view
|

Re: Antwort: Re: Return On Error

Daniel Hornung
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.html

I'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