• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Rob Spoor
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Henry Wong
  • Liutauras Vilda
  • Jeanne Boyarsky
Saloon Keepers:
  • Jesse Silverman
  • Tim Holloway
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Mikalai Zaikin
  • Piet Souris

About one Exception quesion,help

 
Ranch Hand
Posts: 37
IntelliJ IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Ranch Hand
Posts: 447
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but why it say"unreachable code"??

 
Ranch Hand
Posts: 652
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by nik arora:


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



Nik

 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
IntelliJ IDE Firefox Browser Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you anil and Raghavan for your patient explainations a lot!
 
Ranch Hand
Posts: 242
Mac Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
I was her plaything! And so was this tiny ad:
Thread Boost feature
https://coderanch.com/t/674455/Thread-Boost-feature
reply
    Bookmark Topic Watch Topic
  • New Topic