SCJP1.6,SCWCD1.5
My Blog SCJP 5 SCWCD 5
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.
SCJP1.6,SCWCD1.5
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 .....
SCJP1.6,SCWCD1.5
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).......;
SCJP1.6,SCWCD1.5
you are right .. but it happens only with if (either true or false)...or any other .....
My Blog SCJP 5 SCWCD 5
SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
Don't get me started about those stupid light bulbs. |