Forums Register Login

Right way to deal with AssertionError

+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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.
+Pie Number of slices to send: Send
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,
+Pie Number of slices to send: Send
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.
I do some of my very best work in water. Like this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com


reply
reply
This thread has been viewed 11015 times.
Similar Threads
Question on Assertion from KnB's CD
Null Pointer exception
Assertion
Question on assertion error
Question from mock site
More...

All times above are in ranch (not your local) time.
The current ranch time is
Apr 16, 2024 04:50:46.