• 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
  • Tim Cooke
  • paul wheaton
  • Ron McLeod
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Roland Mueller
Bartenders:

jdk1.4 Java Assertion support

 
Ranch Hand
Posts: 37
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi all....I was reading the use of new "assert" keyword included in jdk1.4 in an article published in javaworld. I could not follow one thing that what extra advantage Assertion provides when a similar situation can be handled in a more standard way by java Exception Handling mechanism.
Thanks,
Rahul
 
Sheriff
Posts: 5782
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Rahul,
It is important to note assertions are not replacements for exception handling mechanism. While exception handling helps you handle unexpected runtime conditions, assertions help you test and debug the code that you write as an application programmer. Just like in C++, assertions help you check your assumptions at various points in the program.
The really cool thing about assertions is that it can be turned on/off when you run the program.
You do this with the -ea argument to the java interpreter. This argument by itself enables assertions in all non-system classes. You can also enable on a per-class or per-package basis:
java -ea:com.utils.sorters.BubbleSort // Enable assertions for one class
java -ea:com.utils.sorters // Enable assertions for a package
You can also use the -da argument to selectively disable assertions in classes or packages.
This will enable you to have debug statements in all the critical points without hurting the performance. Sun will eventually modifty the core Java classes to include assertions so that something more meaningful than an ugly stacktrace is displayed when things go wrong.
Hope this helps,
 
reply
    Bookmark Topic Watch Topic
  • New Topic