Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Exception:Unexpected output 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 "Exception:Unexpected output" Watch "Exception:Unexpected output" New topic
Author

Exception:Unexpected output

Santhosh Kumar
Greenhorn

Joined: Nov 15, 2006
Posts: 11
Hi
Anyone can give reason while the code is not showing any exception.I m It compiles fine and prints "Peace"



SCJP 5.0 - 84%<br />SCWCD 1.4 - 86%
Manju Devarla
Ranch Hand

Joined: Dec 14, 2006
Posts: 85
finally block will get executed even the exception is thrown in try block or not..
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
int i = 1 / 0 ; //This line throws a new Exception.


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


SCJP 5.0 95%<br /> <br />The greatest enemy of knowledge is not ignorance, it is the illusion of knowledge.
meena kumari
Greenhorn

Joined: Jan 16, 2007
Posts: 2
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

Joined: Aug 31, 2004
Posts: 11343

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.


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
Javier Sanchez Cerrillo
Ranch Hand

Joined: Aug 02, 2006
Posts: 152
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

Joined: Aug 31, 2004
Posts: 11343

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

Joined: Aug 02, 2006
Posts: 152
Thank you marc, I understand now.
Santhosh Kumar
Greenhorn

Joined: Nov 15, 2006
Posts: 11
Thanks for the Explanation Marc
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception:Unexpected output
 
Similar Threads
Error catch Q
Exception handling problem
why exception is not thrown
Problem in Exception Handing
A runtime exception is throws in the catch clause: