aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes exception doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "exception doubt" Watch "exception doubt" New topic
Author

exception doubt

amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hii

in following question...i don't know why the following answer is write ?

Q
public class Test2{
public static void main(String args[]){
System.out.println(method());
}
public static int method(){
try{
throw new Exception();
return 1;
}
catch(Exception e){
return 2;
}
finally{
return 3;
}
}
}


answer :

compile error.
Error unreachable statement return 1.

Remember if u throw new Exception and if it is not caught then the method should be declared throws Exception.
Or the call to the method should be withing try/catch block.


Thanks and Regards, Amit Taneja
Zhong ZHENG
Greenhorn

Joined: May 02, 2005
Posts: 3
The error message has told you enough info: the "return 1;" is not reachable.
You shouldn't have anything else after a throw clause.
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Amit,

Was the part of your post that started with "Remember if u throw new Exception" your own comment on the example, or was that still part of the answer you were quoting?

Anyway, the point about Exceptions having to be handled or declared is valid - but in this case, the Exception is handled - it is within a try block, and there is an appropriate catch.

The real compiler error in this example is the "unreachable statement", that comes about because, if you trace the logic, there is no hope of the line "return 1" ever being reached. Once the try block is reached, it is guaranteed that the line "throw new Exception()" will run, which will skip the rest of the try block, every time this program was run.

The compiler makes an attempt to discover situations like this, to point out what are likely to be coding problems - no point in writing the code if there's NO chance of it being reached - the compiler assumes you must not have meant to write it that way. But - depending on the compiler - the compiler only goes so far trying to save you from yourself. The lines in bold below would make it compile (unless I've made a typo, or it's a really clever compiler).



I'm not sure if there's something in the language spec that spells out how far a compiler has to go to try to find "unreachable" code - this example might not be enough - you might need to move the "myFlag" variable out of the method (turn it into a class variable), so there's some chance of it being false. I think I remember that "if (true)" is not good enough, and maybe that I've seen an example where "if (true == true)" was also not good enough.

Maybe someone else can shed more light on just what it takes for the compiler to determine that certain code is "reachable"?

-- Jon
amit taneja
Ranch Hand

Joined: Mar 14, 2003
Posts: 810
hi

what i don't understand is why the exception is not cought when the catch clause is there ...

to be honest i don't thing why the compiler complains that "return 1" will never reach.... ya i know it will never reach beacuse throw statement is before the that and it should throw the exception which catch statement must catch up. so return 1 will never reach ( but why complier complains about it )...
infact the code must return "3" ..

this is the thing which i m not able to understand..?
that why complier complains about unreachable code when throw statement shift execution to catch block and
why catch block is not exectuting ?
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

the compiler was complaining because there was no way the return statement could be executed.

If you write this :


Even though the return would still never be reached, it would compile now.
It tells you a bit on how far the compiler will go to look for unreachable code...

Alex
Zhong ZHENG
Greenhorn

Joined: May 02, 2005
Posts: 3
Well, i am confused...

You wrote:
ya i know it will never reach (AND THAT IS NOT ALLOWED) beacuse throw statement is before the that and it should throw the exception which catch statement must catch up. so return 1 will never reach ( but why complier complains about it (BECAUSE UNREACHABLE CODE IS NOT ALLOWED))...
Zhong ZHENG
Greenhorn

Joined: May 02, 2005
Posts: 3
By the way, i'd like to ask another question about the finally clause. Take a look at the modified version of Amit's code:

The above program prints 3 on the screen. But what is the output of the following program:

Well, the real output is a little suprising to me:

Anybody may explain this? Thanks in advance.
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

If you get into the brain of the compiler, when he is looking for unreachable code, if he encounter conditional statements, he has to evaluate the result of the condition and he can't find out at this time.

The only time he is able to tell if the code is unreachable, is when there are no condition, no possible ways (for what he knows) that the code can be reached. We are smarter than the compiler and we know for a fact that "if(true)" will later on be evaluated to true, so it will always go in and thus will never reach the return statement.

Alex
Alex Belisle Turcot
Ranch Hand

Joined: Apr 26, 2005
Posts: 516
Hi,

for the finally, I can give it a try, but I don't think I will word it the official best way..

No matter what, your finally block will always be executed.
So if you try to exit your try-catch block, as a very last action, it will jump to finally. But, remember its really as a last action that the compiler is jumping to finally.

I assume the compiler has already evaluated "return count;" to "return 0;"

regards,
Alex
[ May 02, 2005: Message edited by: Alex Turcot ]
Arun Kumaar
Greenhorn

Joined: Oct 22, 2004
Posts: 20
I have a query regarding the program,

1. Commenting the return statement in the catch block you get an compile time error, even substituted with a System.out.println, or left blank

Is it mandatory that catch should have return or some statements with in the {}

Thanks
Arun


If I could melt your heart<br />We'd never be apart<br /> <br />Frozen --- Madonna
Jon Egan
Ranch Hand

Joined: Mar 24, 2004
Posts: 83
Arun,

Is it mandatory that catch should have return or some statements with in the {}


No, not at all. Sometimes, the apropriate thing to do in the case of an Exception is.... nothing at all. (One common example is in the catch for InterruptedException when calling wait() or sleep().) So, the compiler won't give you an error simply because your catch {} is empty - it has to trust your judgement as to how the Exception is best handled, including not handling it.

What error does the compiler give if you comment out that "return" statement from the catch {}? That will give a hint as to why the compiler gives an error. I suspect it is something like "method might not return a value". So it's not the empty catch block, it's that you are removing the return statement.

*** gibberish removed here
sorry, I had something here that I had "proved" to myself erroneously. I was looking at one of the bits of code above, but copying/pasting/playing with another, and didn't notice they weren't the same case. I drew a bad conclusion.
*** end gibberish removed here


Alex: good luck in 20...19...18 days I put off SCJP for over 1 year, until 2 days before my voucher expired. That strategy worked well for me - I got a 95%, and I can only attribute that to my total lack of focus and concentration

Added:
I meant to make one other point - if you comment out that return in the catch block, but also add a line AFTER the try/catch/finally that returns a value, then the compiler is happy - it sees that it is possible to reach a "return" statement, without counting on the finally block, even if the Exception gets thrown.

-- Jon

[ May 03, 2005: Message edited by: Jon Egan ]
[ May 03, 2005: Message edited by: Jon Egan ]
soumya ravindranath
Ranch Hand

Joined: Jan 26, 2001
Posts: 300
Hi,



As above, if the return statement is moved to finally block, the compiler compiles fine but issues a warning - "finally clause cannot complete normally".
What is the significance of this ?!

thanks,
Soumya.
[ May 03, 2005: Message edited by: soumya ravindranath ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: exception doubt