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

Exception

Rippon Jalali
Greenhorn

Joined: Aug 25, 2006
Posts: 23
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

Joined: Aug 03, 2002
Posts: 7729
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


Ask a Meaningful Question and HowToAskQuestionsOnJavaRanch
Getting someone to think and try something out is much more useful than just telling them the answer.
Rippon Jalali
Greenhorn

Joined: Aug 25, 2006
Posts: 23
Barry i admit my mistake and have done the needfull.Now can you help me with my question.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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 ]

"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

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."
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception
 
Similar Threads
Exception
Infinite loop in Exception ??
somebody pls explain why this code does not give an exception
why this is compiling fine?
one doubt