Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception:Unexpected output

 
Santhosh Kumar
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • 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"

 
Manju Devarla
Ranch Hand
Posts: 85
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
finally block will get executed even the exception is thrown in try block or not..
 
Javier Sanchez Cerrillo
Ranch Hand
Posts: 152
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
int i = 1 / 0 ; //This line throws a new Exception.


Where is the Exception thrown by this line!!!???
 
meena kumari
Greenhorn
Posts: 2
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Java Mac Safari
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you marc, I understand now.
 
Santhosh Kumar
Greenhorn
Posts: 11
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the Explanation Marc
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic