This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Assertion question Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Assertion question" Watch "Assertion question" New topic
Author

Assertion question

Raj Puri
Ranch Hand

Joined: Apr 24, 2003
Posts: 189
I have:
assert(i>5);
j = 10;
Now I put the code in production disabling assertion. How will the application get affected if infact there was a bug in my assumption that i > 5 . This could have bad affects on programs. How does Java handle this? I guess Java does not care, onus of asserting is yours?
Anupam Sinha
Ranch Hand

Joined: Apr 13, 2003
Posts: 1088
Hi Rajesh
Assertions are a mechanism for the tester/progammer/anyone concerned to check that the code is fine during testing. In case later you find a bug you can simply enable assertions and check if there is really a problem with your assumption.
Roger Chung-Wee
Ranch Hand

Joined: Sep 29, 2002
Posts: 1683
I would have thought it quite valuable to enable assertions for production. The extra runtime cost is small or zero - remember, AssertionError will only be thrown if the program does something like break an internal invariant.


SCJP 1.4, SCWCD 1.3, SCBCD 1.3
Raj Puri
Ranch Hand

Joined: Apr 24, 2003
Posts: 189
To have assertion disabled is a big risk, considering that there is no way one can be 100% sure that what you assert is really always true or there is no program error. Seems once assertion is used in development and disabled there is no place to go except to see system getting broken!!
Jose Botella
Ranch Hand

Joined: Jul 03, 2001
Posts: 2120
I do not agree with having exceptions always enabled.
The main situations in which assertions are not appropiate are related the fact that they might not be enabled.
If assertions were intended to be always on, they are useless; we could use exceptions or print statements. I think their advantage is that they can be used while developing/testing, and, in case of problems on production time can be enabled as well.


SCJP2. Please Indent your code using UBB Code
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Assertion question
 
Similar Threads
Assertion diffculty
assertions...
Assertions in JSP
xUnit - assertion failures
assert