• 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 wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Exception:Unexpected output

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
Anyone can give reason while the code is not showing any exception.I m It compiles fine and prints "Peace"

 
Ranch Hand
Posts: 85
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
finally block will get executed even the exception is thrown in try block or not..
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
int i = 1 / 0 ; //This line throws a new Exception.


Where is the Exception thrown by this line!!!???
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
class Test {
static String raid ( ) {
try
{
throw new Exception ( ) ;
}
catch ( Exception e )
{
int i = 1 / 0 ;
return " Error " ;
}
finally
{
return " Peace " ;
}
}
public static void main ( String args [ ] ) {
System . out . println ( raid ( ) ) ;
}
}


If there is a return statement in try block or catch block, the finally block executes right after the return statement is encountered, and (more importantly) BEFORE the return executes.

Therefore finally block starts execute right after the return statement in catch block. But finally block itself has return statement which will end execution of the method by returning string "peace". (I actually tried by changing return "peace"; in finally block to System.out.println(" "), i got the expected runtime exception i.e ArithmeticException.)

If i'm wrong correct me . thanks.
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Javier Sanchez Cerrillo:
int i = 1 / 0 ; //This line throws a new Exception.

Where is the Exception thrown by this line!!!???


If you add a couple lines of code around this, you will see.
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks marc, but I still don't understand, check this code:



1.- I'm amazed why the compiler doesn't complain for a missing return statement!!!.

Once I execute this code the exception on line 1 stops the program. As expected. But....:

2.- If I replace line 2 for: return "Finally", the program compiles and run fine and its output is:
Finally

Why the exception is not caught this time?

Does it have something to do to what meena kumari pointed out?
[ January 18, 2007: Message edited by: Javier Sanchez Cerrillo ]
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Javier Sanchez Cerrillo:
...
1.- I'm amazed why the compiler doesn't complain for a missing return statement!!!.

Once I execute this code the exception on line 1 stops the program. As expected. But....:

2.- If I replace line 2 for: return "Finally", the program compiles and run fine and its output is:
Finally

Why the exception is not caught this time?

Does it have something to do to what meena kumari pointed out?


In your example, an exception will always be thrown, so the method is not required to return normally. But if you make the exception a possibility instead of a certainty, then the compiler will require a return statement. For example...

And yes, as meena pointed out, if the finally block includes a return statement, then the method will return normally (i.e., control will be passed to the point at which the method was called). And in this situation, whatever was caught will basically be "lost." However, I would point out that this is probably a misuse of finally, because if an exception is thrown, do you really want your method to return? The finally statement is basically for cleanup that should be performed even if the method does not return normally.
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you marc, I understand now.
 
Santhosh Kumar
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the Explanation Marc
 
reply
    Bookmark Topic Watch Topic
  • New Topic