wood burning stoves 2.0*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes throw/catch exception 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 "throw/catch exception" Watch "throw/catch exception" New topic
Author

throw/catch exception

Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
http://www.jiris.com/
mockexam 2 Question 27:
---------------------------------
import java.io.*;
public class Test027
{
public static void main(String args[]) {
System.out.println(method());
}
static int method() {
try {
throw new IOException();
}
catch (IOException ioe) {
System.out.println("0");
return 0;
}
finally {
System.out.println("1");
return 1;
}
return 2;
}
}
------------------------------------
During compiling, error occurred with "unreachable statement, return 2". I thought new IOException was thrown in try block, then catch block caught it and returned "0". Apparently it did not work like that. Can any one explain this? Thank you very much!
Moya
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
return 2 is unreachable because the return in the finally will always be executed.


Associate Instructor - Hofstra University
Amazon Top 750 reviewer - Blog - Unresolved References - Book Review Blog
Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
Can you briefly explain how the code compile step by step? I did not get your points. Thanks again. I appreciate it.
Moya
Anthony Villanueva
Ranch Hand

Joined: Mar 22, 2002
Posts: 1055
Order inside method() is:
1. code inside try block
2. code inside catch block
3. code inside finally block
4. code after //2 if there was no return statement inside finally block
Please note both return statements execute (i++ is used to check this), although the method actually returns on // 1
Billy Talton
Greenhorn

Joined: Sep 17, 2002
Posts: 19
Moya,
I completely disagree with Thomas.. okay sort of. What I think should be noted/expounded is that when you purposedly throw an exception, have a <B>return</B> a value in the catch <I>and</I> then a finally clause, you effectively have a if/else logic for handling the exception (and exiting he method) which guarantees that anything after the finally will not be reached. Try this... I removed the <B>return</B> from the catch and finally. I will now compile.
try
{
throw new IOException();
} catch (IOException ioe)
{
System.out.println("0");
//return 0;
} finally
{
System.out.println("1");
//return 1;
}
return 2;
______
Billy Talton
billy.talton@cornerstone.net
Barkat Mardhani
Ranch Hand

Joined: Aug 05, 2002
Posts: 787
Hi Billy:
I guess you and Thomas are saying same thing. The mere fact that finally has a return statement suffices the fact that any statements after finally will never be reached....
[ September 26, 2002: Message edited by: Barkat Mardhani ]
Moya Green
Ranch Hand

Joined: Jan 24, 2002
Posts: 49
Clear.
Thank you very much, everyone! You ar ethe best!
Moya
Thomas Paul
mister krabs
Ranch Hand

Joined: May 05, 2000
Posts: 13974
Originally posted by Billy Talton:
Moya,
I completely disagree with Thomas.. okay sort of. What I think should be noted/expounded is that when you purposedly throw an exception, have a <B>return</B> a value in the catch <I>and</I> then a finally clause, you effectively have a if/else logic for handling the exception (and exiting he method) which guarantees that anything after the finally will not be reached.

You are mistaken. The return in the finally will ALWAYS be executed. The only time it isn't is if the catch has a System.exit().
Try this:

The method will return 1, the value from the finally and not 0, the value from the catch.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: throw/catch exception
 
Similar Threads
unreachable statements
Return from catch block
Exception
Cannot resolve symbol...
Exception