aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes About one Exception quesion,help 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 "About one Exception quesion,help" Watch "About one Exception quesion,help" New topic
Author

About one Exception quesion,help

Zhao zhenhua
Ranch Hand

Joined: Jul 17, 2006
Posts: 37


a. Compile error unreachable code
b. Comepile error: cant throw two exceptions at the same time
c. Compile error: both Exceptions need to be handled in try catch block
d. Runtime error
e. No errors. Compiles fine without warnings

the answer is a,why? Who can you rancher help me?Thanks


Head first Servlet and JSP
anil kumar
Ranch Hand

Joined: Feb 23, 2007
Posts: 447
Hi

What ever be the statements after the keyword throw will not be executed and the compiler knows this,So it will give compile time error.


Thanks

Anil Kumar
Zhao zhenhua
Ranch Hand

Joined: Jul 17, 2006
Posts: 37

but why it say"unreachable code"??

Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Originally posted by Zhao zhenhua:
but why it say"unreachable code"??



Hi,
Think what happens when exception is thrown you will get the answer?



Nik
Nik Arora
Ranch Hand

Joined: Apr 26, 2007
Posts: 652
Originally posted by nik arora:


Hi,
Think what happens when exception is thrown you will get the answer.



Nik
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Hello Zhao zhenhua ,

Soon after the compiler sees the first statement which is throwing a new exception of type "ex" (throw new ex() , it will be sure that an exception would be thrown at runtime and therefore the immediate-next statement would NOT be executed. That means, the immediate-next statement is considered to be NOT REACHABLE.

HtH.


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
Zhao zhenhua
Ranch Hand

Joined: Jul 17, 2006
Posts: 37

Thank you anil and Raghavan for your patient explainations a lot!
khushhal yadav
Ranch Hand

Joined: Jun 20, 2007
Posts: 242

Even if you write a return statement after the throw statement..
That would also give you compile-time error for being unreachable code..


rgrds,
Khushhal
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3355

Not just a return statement, any executable statement after the throw or return statement would be treated as Unreachable as the term itself indicates.

See the sample program:



If you uncomment any of the lines line2 or line3 in the method1(), the compiler would give an error saying that the line below the throw statement is unreachable.

Whereas, in method2() it just executes fine without any errors since the exception is caught and compiler is not insisted to care about it. And the executable statement is safely placed outside the catch block.

If the same method2() has been called in the try block of main() method after the call to method1() - uncomment the line1, the compiler will not give the "Unreachable code" error because of the reasons said in the previous paragraph, but at runtime it will not be called at all since the control will be transferred to the catch block.

HtH.
[ June 22, 2007: Message edited by: Raghavan Muthu ]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: About one Exception quesion,help