• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Exception

 
Rippon Jalali
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The problem in the code below is that catch clause in the called method rethrows exception,and main method neither handles nor declares the exception.So shouldn't it give compiler error?but it isgiving answer 3,

public class Test3{
public static void main(String args[]){
System.out.println(method());
}
public static int method(){
try{
throw new Exception();
}
catch(Exception e){
throw new Exception();
}
finally{
return 3;
}
}
}
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello "Rippon"-

Welcome to JavaRanch.

On your way in you may have missed that we have a JavaRanch Naming Policy for displayed (screen) names. Your displayed name must consist of a first name (or an initial), a space, and a family name (in that order) and not be obviously fictitious. Since yours "Rippon", does not conform with it, please take a moment to change it, which you can do right here.

Posters with nonconforming displayed names will be locked out of JavaRanch after a few posts using those names.

Thanks
-Barry

(NR) - search tag
 
Rippon Jalali
Greenhorn
Posts: 23
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Barry i admit my mistake and have done the needfull.Now can you help me with my question.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think it is because you have a return statement in your finally block, which allows the method to complete normally by returning a value. In other words, the new exception that's thrown in the catch block does not end up causing the method to complete abruptly.

Note that without this return statement (e.g., if the return type is changed to void), the new exception is required to be declared or handled.

Interestingly, the new exception seems to be "lost." But section 14.20 of the JLS states...
A try statement can throw an exception type E [if]... Some catch block of the try statement can throw E and either no finally block is present or the finally block can complete normally...

In this case, the catch block is throwing an exception and the finally block completes normally, so I would expect the try statement to be throwing the new exception.

A similar situation is detailed in Bruce Eckel's Thinking in Java, under the section Pitfall: the lost exception.
[ January 05, 2007: Message edited by: marc weber ]
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I still think I'm missing something in the JLS section quoted above, so if anyone sees what that is...

But regardless, I should point out that this situation is resulting from a likely misuse of the finally statement. Remember that finally executes in all cases -- whether or not an exception occurs. So would you really want your method to return if an exception occurs?

In my post above, I cited a section from Eckel's TIJ, "Pitfall: the lost exception." But the section directly preceding that is also informative: "What's finally for?"...
The finally clause is necessary when you need to set something other than memory back to its original state. This is some kind of cleanup like an open file or network connection, something you�ve drawn on the screen...

Emphasize the word "cleanup."
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic