aspose file tools*
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Exception" Watch "Exception" New topic
Author

Exception

Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Here is the code:



This gives compiler error at last staement .. says statement unreachable.

My undrestanding of exceptions say that -- its okay if the exception is either declared or caught. Here in the main method, the exception e1 is thrown. What I don't understand is why the last statement is unreachable? Please explain.
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Want to add something more--
What I understand is -- that if the exception is declared to be thrown, it'll work fine even if there is no catch (or not handled). Am I correct here?
Or is it that -- main() method should not leave any exception unhandled?
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Guptajee,

Give definition of the class MyException.
I assume it like this:-




The unreachable code error is due to the "throw e1" that you have placed in the finally block. Your last statement is not statement because there is no try catch block for the "throw e1" in the finally. If you thought that your just up to finally catch block will handle it, it would wrong, it would never return to that; main docks the MyException thrown in the main() method, so the exception thrown in the finally block goes unhandled although docked by the main(). JVM throws the exception at run time and that is why your last statement is not reachable.

You place the "throw e1" of the finally block inside try catch block, your last statement will get executed. Because all the exceptions would be handled properly and there wont be any exceptional case at run time. (Unhandled exception).



Answer to the supplementary question:

If a method declares it throws the exception, it need not to put the "throw exceptionname" in the try catch block.

But the calling method must handle it or declare it to be thrown as you did with main, main declares that it throws MyException (while calling method).
There is no necessity that main handles all the exceptions, it can declare that it throws the exception, and it will do. Anyways finally there is the main() method in the bottom of the stack, if main docks the exception nobody is there to catch it.


Thanks and Regards,
cmbhatt
[ March 30, 2007: Message edited by: Chandra Bhatt ]

cmbhatt
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Ya, thats right. We need to define MyException first. Here is the modified code:
[CODE]
class MyException extends Exception{}

class Test{
void f() throws MyException{
throw new MyException();
}

public static void main(String[] args) throws MyException{
MyException e1 = null;
Test t = new Test();
try{
t.f();
}catch(MyException e){
e1=e; System.out.println("catch");
}finally{
System.out.println("finally");
throw e1;
}
System.out.println("End");
}
} [/CODE

So, you mean to say that if we put "throw e1;" in a try catch block the program should compile?
I tried that -- but I got the same error -- Statement unreachable.
Please explain.
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Ya, thats right. We need to define MyException first. Here is the modified code:


So, you mean to say that if we put "throw e1;" in a try catch block the program should compile?
I tried that -- but I got the same error -- Statement unreachable.
Please explain.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi GuptaJee,

See above the edited code please.
Let me know if anything missing there!!!



Thanks and Regards,
cmbhatt
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Originally posted by Loveleen Gupta

So, you mean to say that if we put "throw e1;" in a try catch block the program should compile?
I tried that -- but I got the same error -- Statement unreachable.


It is not true, code should compile and run fine
Use this code:



Thanks and Regards,
cmbhatt
Chris Jebaraj
Ranch Hand

Joined: Sep 19, 2003
Posts: 34
Hi Gupta,

You are throwing the caught exception in the finally block. That means your method ends there. There is no chance of statements after that could execute. That's why you are getting that compilation error.


JebaRaj.<br /> MCP, SCJP 1.5 <br />----------------------------<br /> THOUGH TIMES TEACH TRUST. <br />----------------------------
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Okay. Trying to understand what you people said.
Ya, Chandra, it worked with throw e1 in try-catch..but not in try-finally.

So, is it that the main should not leave any exception uncaught?
What I think is that, since main has declared that it will throw an exception, it is not necessary to catch the exception for the program to work fine.
Please help.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Guptajee,

catching the exception and declaring it is different. if main declares that it throws particular exception, you need not to place the code in the try catch block that throws the exception; because main declares that it throws it. Your first statement goes wrong "main must catch every exception".

You have only two options anywhere:
1- catch it using try catch block ofcourse.
2- declare it in the method that is throws the exception so that that need not to place the code in the try catch block; this phenomenon is called ducking;

Thanks and Regards,
cmbhatt
Lovleen Gupta
Ranch Hand

Joined: Feb 26, 2007
Posts: 63
Originally posted by Chandra Bhatt:
Hi Guptajee,

catching the exception and declaring it is different. if main declares that it throws particular exception, you need not to place the code in the try catch block that throws the exception; because main declares that it throws it. Your first statement goes wrong "main must catch every exception".

You have only two options anywhere:
1- catch it using try catch block ofcourse.
2- declare it in the method that is throws the exception so that that need not to place the code in the try catch block; this phenomenon is called ducking;

Thanks and Regards,
cmbhatt


Hi Chandra,
Thanks for replying constantly. :-)
All right..So, like you said -- that is you declare that an exception will be thrown, there is no need to put that in try-catch.
Similar, is the situation with the original code..main() declares that it'll throw MyException. and it does throw MyException e1 in finally block.
Then, why was there a need to put throw in try-catch for the code to compile properly.

Regards,
Lovleen.
Chandra Bhatt
Ranch Hand

Joined: Feb 28, 2007
Posts: 1707
Hi Guptajee,



Similar, is the situation with the original code..main() declares that it'll throw MyException. and it does throw MyException e1 in finally block.
Then, why was there a need to put throw in try-catch for the code to compile properly


That is not the actual reason why does your code fail to compile. What happens, when there is a method call of method itself, and it is sure by looking at the code that the exception will be thrown as "throw exceptionname", not due to some runtime reason as file read failure or so.
compiler issues error that particular code statement is not reachable. Why I did suggest you to put the "throw e1" in the try block so that your last statement could be reached to be executed because you handled the exception and dont dock it (as your main already declares that it throws MyException).

The matter of fact is, if main() too docks (main is the downmost element in the stack), the exception, exception can't stay cool, it is thrown and it will be thrown and your program terminated "exceptionally", so therefore because compiler knows that your program will be terminated exceptionally it has given you error that your last statement is not reachable.

Got it?

A lot discussion on this issue may fix this topic permanently in your brain.

Thanks and Regards,
cmbhatt
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Exception