• Post Reply Bookmark Topic Watch Topic
  • New Topic

try catch finally for Java Puzzlers Authors  RSS feed

 
max sacker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
class TestTryCatch
{
public static void main(String[] args)
{
TestTryCatch me = new TestTryCatch();

try
{
System.out.println("test=" + me.test());
}
catch(Exception exx)
{
exx.printStackTrace();
}
}

public String test() throws Exception
{
try
{
System.out.println("try");
int i = 0;
int j = 1/i;
}
catch(Exception e)
{
//log exception.
e.printStackTrace();
throw e; // rethrow exception so that caller can handle it.
}
finally
{
try
{
///something
System.out.println("finally try");
}
catch(Exception ex)
{
ex.printStackTrace();
}
return "Wonder"; // 1
}
//2
}
}


I found strange behaviour in this code segment.
If line //1 is moved to//2 point it works properly as expected. I.e. the exception is propagated to the calling method.
But when it is at line //1 the exception is not propagated to the calling method.

I don�t understand the reason.
Pls explain.

- Max
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24217
38
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Max,

Welcome to JavaRanch!

Any code that you put in a finally block is guaranteed to execute after the try and/or catch blocks are done. If you throw an exception from the finally, then that exception will propagate rather than any previous one. Likewise, if you "return" from a finally block, then the method will return normally.

Returning from "finally" is strongly discouraged because, as you note, it can hide exceptions.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The compiler should even provide you a warning if you do so.
 
Jim Yingst
Wanderer
Sheriff
Posts: 18671
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
[Ilja]: The compiler should even provide you a warning if you do so.

I remember seeing such a warning in the past; I belive it was new behavior with the 1.5 compiler. However I can't reproduce it right now. I may be misremembering, or it may be something that only shows up in certain specific situations. The following compiles just fine for me using JDK 1.5.0_04:

[ August 12, 2005: Message edited by: Jim Yingst ]
 
Joshua Bloch
Author and "Sun God"
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jim,

I believe that my brother put this warning into the compiler, but the powers that be made him take it out until it could be selectively disabled. But I could be wrong. I suspect he'll tell us.

Josh

Originally posted by Jim Yingst:
[Ilja]: The compiler should even provide you a warning if you do so.

I remember seeing such a warning in the past; I belive it was new behavior with the 1.5 compiler. However I can't reproduce it right now. I may be misremembering, or it may be something that only shows up in certain specific situations. The following compiles just fine for me using JDK 1.5.0_04:


[ August 12, 2005: Message edited by: Jim Yingst ]
 
max sacker
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks to u all for clearing my doubt.

- Max
 
Neal Gafter
javac author
Greenhorn
Posts: 12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Use -Xlint:all to enable all of the excellent warnings that javac can provide.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!