• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

About one Exception quesion,help

 
Zhao zhenhua
Ranch Hand
Posts: 37
Firefox Browser IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

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
 
anil kumar
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 37
Firefox Browser IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
but why it say"unreachable code"??

 
Nik Arora
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 652
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by nik arora:


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



Nik
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Zhao zhenhua
Ranch Hand
Posts: 37
Firefox Browser IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you anil and Raghavan for your patient explainations a lot!
 
khushhal yadav
Ranch Hand
Posts: 242
Java Mac Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Even if you write a return statement after the throw statement..
That would also give you compile-time error for being unreachable code..
 
Raghavan Muthu
Ranch Hand
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic