• 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

Proper and Improper Use Of Assertions

 
Ranch Hand
Posts: 48
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Section 2 has an objective that says this:
"Develop code that makes use of assertions, and distinguish appropriate from inappropriate uses of assertions."

I've always assumed that assertions were designed to check for things that are never supposed to happen, but after reading "The Complete Java 2 Study Guide," apparently it's not simple. There are specific times when using assertions is good and bad. Although the book says a bunch of stuff it doesn't all that much sense.

Can anyone provide some clarity on the topic.
 
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
An Assertion should not do something that changes the state of data, is an example of improper use. I should just check, but not do anything to anything. Kind of like the Assertion should look but not touch.

Assertions shouldn't be used to do actual needed work, like above, but also things like checking to make sure the correct parameters are passed. A straight check like using ifs and then throwing an IllegalArgumentException, something like that. I am not sure if that is the right Exception to throw or is it something else. I can't think of that on the top of my head right now.

Mark
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Assertion is used to test our Assumptions during the development. Incase of failuare of any condition AssertioError is thrown. Normally this facility is disabled in the production environment.
You are not expected to catch this error in try block and provide recovery in catch block. You are expected to go back and rectify the error prone code.

Developers are tempted to catch this error (As subclass of Error ) but should avoid this tempation.

Rajesh
 
Mark Spritzler
ranger
Posts: 17347
11
Mac IntelliJ IDE Spring
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
And to add to Rajesh's point and what I had said. If you have some action occur that changes state in your assertion that your app relies on, then when you put it in production and turn assertions off, that action would not occur and your app then breaks.

Mark
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic