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.
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 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 .....
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).......;
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.
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...