• 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
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

asserts in 1.4

 
Ranch Hand
Posts: 280
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have not had a chance to try it yet. How are assert statements in 1.4 used in code? I understand and can use them in C++. Are they used the same? What is the syntax?

Thanks,
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
http://www.onjava.com/pub/a/onjava/2002/03/06/topten.html
You should find it in there alongwith he other new features.
 
Author
Posts: 67
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you use assertions in your code, you must compile with the option -source 1.4,(1.3 is the default), and execute with the option -enableassertions (or -ea).
The syntax is very simple. The basic assertion statement is:
assert boolean_expression;
If boolean_expression evaluates to false, the program will terminate by throwing an AssertionError exception.
You can also use the form:
assert boolean_expression : expression;
where expression can be an expression of any type but typically will be a String object. If boolean expression is false an exception of type AssertionError is thrown, but this time the result of evaluating expression will be passed to the AssertionError constructor(there are constructors for all the primitive types plus Object).
Basically the effect of an assertion is the same as in C++.
 
reply
    Bookmark Topic Watch Topic
  • New Topic