*
The moose likes Java in General and the fly likes Right way to deal with AssertionError Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "Right way to deal with AssertionError " Watch "Right way to deal with AssertionError " New topic
Author

Right way to deal with AssertionError

Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9962
    
163

Hi,
We have a production level application, which we are trying to integrate with a different module. One of the issues that we are running into is that the code in the module, has 'asserts'. Now, because of this there are quite a few occasions when we have seen the java.lang.AssertionError being thrown. Our problem is that in our application, we are catching java.lang.Exception and on catching the exception, we do some additional logic. Now, since the AssertionError extends from Throwable, this never gets caught and we are not seeing the expected behaviour in our application.

My question is, Is it a right practice to throw AssertionError in production level code. Are applications supposed to catch them? I wanted to know this so that we can either change the code in the module that we are integrating our application with, or just start catching Throwable instead of Exception in our application, so that even the AssertionError is caught.

Thank you.


[My Blog] [JavaRanch Journal]
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

AssertionErrors are thrown when assertions fail. Assertions fail because something the programmer believed to be true turns out not to be true. That means there is a bug in the code. If you see an AssertionError, find and fix the source of the problem right away!

If you catch an AssertionError and continue, then you have no idea whether the application is working correctly anymore. From that point forward, it's entirely possible that everything the application does is garbage.


[Jess in Action][AskingGoodQuestions]
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14074
    
  16

Do you know exactly what assertions are and what they are used for?

An example of the use of assertions is to check pre- and postconditions in parts of your code. A method may for example have an assert to check that the parameters that were passed to it are valid.

If an AssertionError happens in your application, it means that there is a bug somewhere in your software. You might be passing invalid arguments to a method somewhere, or you're doing something else so that the software gets into an undefined, invalid state.

AssertionErrors should NEVER happen in production software. If an AssertionError happens, it means that something is seriously wrong in your software and you should debug it.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
Archies Gomes
Ranch Hand

Joined: Jun 19, 2005
Posts: 34
Hi
Assertions are used at production level just for checking wheather everything is going on as we expected or not.If you see an assertion Error being thrown ,then you need to go and catch hold of the bug by yourself manually...Do not let the Throwable to interfere with this ,ie do not catch these type of Errors as the main purpose of Assertions would be lost.Its not an exception its an KNOWN ERROR (Exceptions are something which we cant predict)So let us not run from the battlefield rather attck the bug and let it go out of the software.
Great Luck


Thanks,
Jaikiran Pai
Marshal

Joined: Jul 20, 2005
Posts: 9962
    
163

Thank you all for those replies. That makes it clear that AssertionErrors are not meant to be caught. Will fix the appropriate module. Thank you all.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Right way to deal with AssertionError
 
Similar Threads
Question from mock site
Question on Assertion from KnB's CD
Assertion
Null Pointer exception
Question on assertion error