This week's book giveaway is in the OO, Patterns, UML and Refactoring forum. We're giving away four copies of Refactoring for Software Design Smells: Managing Technical Debt and have Girish Suryanarayana, Ganesh Samarthyam & Tushar Sharma on-line! See this thread for details.
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.
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.
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.
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