wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes doubt in try,catch,finally Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "doubt in try,catch,finally" Watch "doubt in try,catch,finally" New topic
Author

doubt in try,catch,finally

Kanchan Narang
Greenhorn

Joined: Jul 07, 2007
Posts: 8
Hi All ,
I m bit confused about the way try ,catch,finally bolcks work.
--- code snippet ----
try {
throw new IOException();

}catch(Exception e){
e.printStackTrace();
}fianlly{
System.out.println("ABC");
}
O/p:
ABC
Exception Trace .
Cor Takken
Ranch Hand

Joined: May 21, 2007
Posts: 58
Originally posted by Kanchan Narang:
Hi All ,
I m bit confused about the way try ,catch,finally bolcks work.
--- code snippet ----
try {
throw new IOException();

}catch(Exception e){
e.printStackTrace();
}fianlly{
System.out.println("ABC");
}
O/p:
ABC
Exception Trace .


It is fairly simple:
The try block contains the code which is ran for business purposes. In this block an error might be thrown (either explicitly (as per your example) or implicitly (another method may throw an error), this error is processed (caught) in the catch block. Here the error is processed, however the finally block will always be run, whether there is an error or not. What you have shown in your output is that the finally block is run before the catch block. I'm not too sure if an processing order is explicitly given in the JVM descriptions, and to be honest I could have sworn that the catch block would be processed before the finally block. This proves the validity of the point that in the finally block there should never be any code which is core to the program-functionality but only general closure methods (as the finally block will also be run if no error has been thrown), and the catch block should no rely on the availability of any resources which are processed in the finally block (as your trial has shown that the finally block may -and perhaps always will- be run before the catch block).


SCJP 1.5
Dolphins are grey, but they dream in colour.
Manfred Klug
Ranch Hand

Joined: Jun 04, 2007
Posts: 377
Hi,

the problem here is that 'e.printStackTrace()' writes to standard error and ' System.out.println' to standard out. Dependent on the environment (e.g. Eclipse), the output from standard out and error may not be printed in the correct order.
sandeep atluri
Ranch Hand

Joined: Apr 19, 2007
Posts: 42
hi,

let me get it straight to you.....

you enclose code that might throw an Exception, in a try block

and you catch whatever the exception is thrown, using a Catch block.
..

So, the way the action takes place....

1: try block code executes and throws any reported Exceptions(if any)..

2: Catch block catches this/these exceptions and matches them with the appropriate catch cases declared.
then executes the code declared in the appropriate catch block.

3: Now comes the finally block....(if there is any, declared....)
this block will be the first to be executed immediately after the try/catch block executes....

Note: no matter if the exception is caught or not... the finally block(if
-declared) will always be executed.

then..........
if there is/are any other code/code blocks.....(after the Finally block) then its their turn to be executed...


So, your example.......

try block throws the exception....

Catch block catches this Exception....

(Since the Exception is caught.... there is no way that the program to be terminated abnormally...)

Now the finally block executes...

(and the control is returned to the main program.....)



Out Put:


IO Exception is thrown by the try block...

Caught by the Catch block...
--- prints the Exception thrown...

And....
Finally... the finally block code is executed...
i.e., ABC is printed...


i.e.,...
"

ABC
"

Note: the Question you gave has the answer in wrong order...

Explanation:

the call to is made first.... so it will be printed first...

then only the finally block will execute ...thereby producing the output as "ABC"


Got it...


Sandeep Atluri
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: doubt in try,catch,finally
 
Similar Threads
finally block vs normal-out-of-try-catch-scope
Exceptions (long one) Your response requested.
Casting Throwbale to Exception
try, catch,finally
exception in finally