Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

throw/catch exception

 
Moya Green
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
return 2 is unreachable because the return in the finally will always be executed.
 
Moya Green
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1055
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 19
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 787
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Clear.
Thank you very much, everyone! You ar ethe best!
Moya
 
Thomas Paul
mister krabs
Ranch Hand
Posts: 13974
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic