• 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:
  • Campbell Ritchie
  • Tim Cooke
  • paul wheaton
  • Liutauras Vilda
  • Ron McLeod
Sheriffs:
  • Jeanne Boyarsky
  • Devaka Cooray
  • Paul Clapham
Saloon Keepers:
  • Scott Selikoff
  • Tim Holloway
  • Piet Souris
  • Mikalai Zaikin
  • Frits Walraven
Bartenders:
  • Stephan van Hulst
  • Carey Brown

throw/catch exception

 
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 1055
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 19
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
[email protected]
 
Ranch Hand
Posts: 787
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Clear.
Thank you very much, everyone! You ar ethe best!
Moya
 
Thomas Paul
mister krabs
Posts: 13974
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
I'm full of tinier men! And a tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic