aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes unreachable statements 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 "unreachable statements" Watch "unreachable statements" New topic
Author

unreachable statements

narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
there is a error in kalid mughal books coding ..
page :409-410

here is the code..



Any exception thrown explicitly either checked or unchecked must be the last statement of the block...

Is this right...



SCJP1.6,SCWCD1.5
Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

Yes this is true that Any exception thrown explicitly either checked or unchecked must be the last statement of the block... but this is in case when the programmer throws the exception by himself..

like


any code below this line will give compiler error. now if you think logically that why this gives you compilation error you will get the answer. After trowing the error the control doesn't comes back to the next line. It goes to the catch block or finally block whichever is there and if both are there it first goes to catch block and then to finally.



In the code snippet which you have give the exceptions are thrown inside if. so there is a condition that the exception will be thrown only if the condition is true. the compiler is smart enough to detect this and in this case you will not get any compiler error. This behaves in the same manner as conditional initialization of local variables.

My Blog SCJP 5 SCWCD 5
narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
Himanshu Gupta wrote:Yes this is true that Any exception thrown explicitly either checked or unchecked must be the last statement of the block... but this is in case when the programmer throws the exception by himself..

like


any code below this line will give compiler error. now if you think logically that why this gives you compilation error you will get the answer. After trowing the error the control doesn't comes back to the next line. It goes to the catch block or finally block whichever is there and if both are there it first goes to catch block and then to finally.



In the code snippet which you have give the exceptions are thrown inside if. so there is a condition that the exception will be thrown only if the condition is true. the compiler is smart enough to detect this and in this case you will not get any compiler error. This behaves in the same manner as conditional initialization of local variables.


you are right .. but it happens only with if (either true or false)...or any other .....

Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

It happens only when Condition is there.. otherwise it behaves in the same manner..
narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
narendra bhattacharya wrote:
Himanshu Gupta wrote:Yes this is true that Any exception thrown explicitly either checked or unchecked must be the last statement of the block... but this is in case when the programmer throws the exception by himself..

like


any code below this line will give compiler error. now if you think logically that why this gives you compilation error you will get the answer. After trowing the error the control doesn't comes back to the next line. It goes to the catch block or finally block whichever is there and if both are there it first goes to catch block and then to finally.




you are right but you are saying the same thing .....in if statement block explicit throw must be the last statement otherwise unreachable code compiler error.....

In the code snippet which you have give the exceptions are thrown inside if. so there is a condition that the exception will be thrown only if the condition is true. the compiler is smart enough to detect this and in this case you will not get any compiler error. This behaves in the same manner as conditional initialization of local variables.


you are right .. but it happens only with if (either true or false)...or any other .....

narendra bhattacharya
Ranch Hand

Joined: Feb 17, 2010
Posts: 66
narendra bhattacharya wrote:
Himanshu Gupta wrote:Yes this is true that Any exception thrown explicitly either checked or unchecked must be the last statement of the block... but this is in case when the programmer throws the exception by himself..

like


any code below this line will give compiler error. now if you think logically that why this gives you compilation error you will get the answer. After trowing the error the control doesn't comes back to the next line. It goes to the catch block or finally block whichever is there and if both are there it first goes to catch block and then to finally.




sir...
you are right but you are saying the same thing .....in if statement block explicit throw must be the last statement otherwise unreachable code compiler error.....

if block is also a block if we specify if (condition){}//this is a block otherwise an implicit one line block like this if(condition).......;


Himanshu Gupta
Ranch Hand

Joined: Aug 18, 2008
Posts: 598

you are right .. but it happens only with if (either true or false)...or any other .....


I will suggest you to give it a try by yourself. Try experimenting using various conditional statements. It will not only build your basic concepts but also provide you new direction to think.
Ankit Garg
Sheriff

Joined: Aug 03, 2008
Posts: 9280
    
  17

narendra please don't put your own statement in quote blocks, it gets confusing as to what you are saying is new or old. Also you don't need to quote a person every time. Quoting is generally used if you want to reply to a specific person or a part of a person's post. You can also use the button to post your reply...


SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: unreachable statements
 
Similar Threads
Forward reference in a method
Khalid pg 333-334
exception handling in instance initializer
Pls Explain few lines of the code.
Forward Referencing Doubt