File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exceptions Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exceptions" Watch "Exceptions" New topic
Author

Exceptions

Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Whats the use of having a return statement in a catch or finally block?
Basically, how does the rpogram flow, once it encounters a return statement?
For exp, in the program below, what will be the output if "Hello.txt" doesn't exist:


Thanks..
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Guptajee,


Posted By Lovleen Gupta
Whats the use of having a return statement in a catch
or finally block? Basically, how does the rpogram flow,
once it encounters a return statement?


Suppose,if exception is thrown, you want to make return statement,
and no other statement execution of the method to proceed. But finally
will execute for sure unless you write System.exit(1) or something that
terminates the program. Suppose you dont write "return -1;" in your catch
clause, even after exception comes, a line that may outside of the try catch
clause will execute and may yield undesirable result. So you wrote "return -1" there.

Got it?


Regards,
cmbhatt


cmbhatt
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Originally posted by Chandra Bhatt:
Hi Guptajee,



Suppose,if exception is thrown, you want to make return statement,
and no other statement execution of the method to proceed. But finally
will execute for sure unless you write System.exit(1) or something that
terminates the program. Suppose you dont write "return -1;" in your catch
clause, even after exception comes, a line that may outside of the try catch
clause will execute and may yield undesirable result. So you wrote "return -1" there.



Got it?


Regards,
cmbhatt


So, in this program, why is -1 printed and not 0?
Please explain.

Thanks.
[ April 12, 2007: Message edited by: Lovleen Gupta ]
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hey Gupta,

System.exit(0); //Normal termination of the program
System.exit(-1); //Supposed to be an abnormal termination of program

But as you return anything from the method, generally -1 shows that something
went wrong. You can even return 3, 13 , and 43 if you treat as it is abnormal
return from the method. (It is upto you). But general practice says negative returned value, something abnormal happened there.


Got it Guptajee?

Regards,
cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exceptions
 
Similar Threads
try catch block
problem with catch and finally
exCeption
static methods
finally block executes right after the return statement?