This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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
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.
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.