• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Liutauras Vilda
  • Paul Clapham
Sheriffs:
  • paul wheaton
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Piet Souris
Bartenders:
  • Mike London

assertions in try-catch blocks

 
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Ranch Hand
Posts: 518
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that option D is not true. There is no rule that keeps you from putting assert statements inside a try/catch.
 
Ranch Hand
Posts: 2023
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The assertion is specifically designed for debugging, no try-catch block is necessary.
 
Ranch Hand
Posts: 10198
3
Mac PPC Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Catherine,

Even I picked the same result as B&E and got the same feedback that B&D are correct. We can put the assert condition in a try catch block though it is not necessary.

Regards,
Jothi Shankar Kumar. S
 
catherine powell
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I agree that logically it doesn't make any sense to put an assertion in a try-catch. However, the way that option D was phrased, I thought it was an issue of legal vs. illegal code.

Hopefully, I won't see a question worded in a similar way on the real test.

Thank you guys very much for your help...Catherine

BTW, this question is from the 1.4 Diagnostic Test, Question #33.
 
Ranch Hand
Posts: 1274
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Ranchers,

Catherine wrote

I agree that logically it doesn't make any sense to put an assertion in a try-catch. However, the way that option D was phrased, I thought it was an issue of legal vs. illegal code.

Hopefully, I won't see a question worded in a similar way on the real test.


Yes, that's a silly question from whizlabs and should be approved.

And yes, putting an assertion in a try catch block is totally against the sense of assertions:
legal, but not appropriate, but what's "may not be" ?
It should be clear if they meant "legal" or "appropriate". And also it's not appropriate in the public main() method.




By the way, did you spot the paradoxon in answer E ?





Yours,
Bu.
reply
    Bookmark Topic Watch Topic
  • New Topic