Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

unreachable statements

 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...


 
Himanshu Gupta
Ranch Hand
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It happens only when Condition is there.. otherwise it behaves in the same manner..
 
narendra bhattacharya
Ranch Hand
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 598
Android Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 9509
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic