Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

doubt in try,catch,finally

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 .
 
Ranch Hand
Posts: 58
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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).
 
Ranch Hand
Posts: 377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 42
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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...
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic