From Whizlabs 1.4 Mock...
Which of the following statements about assertions are true? (choose 2)
A. Assertions are by default enabled in JDK 1.4, during compilation and runtime.
B. In assert syntax - assert expression1; the expression1 must be a boolean expression.
C. In assert syntax - assert expression1; the expression1 must be an expression resulting in an int.
D. An assert statement may not be enclosed in a try-catch block.
E. Only option B is correct.
I picked B & E. Whizlabs states B and D are correct.
I agree with B but don't understand why D is correct.
The following codes compiles (using -source 1.4 switch) and runs as expected (using -ea):
public class AssertTC{
public static void main(
String[] args){
try{
int x=1;
assert (x==2);
}catch(Throwable t){
System.out.println("caught!");
}
}
}
Can someone please explain why D should be marked correct?
Thank you very much in advance...Catherine