• 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

Right way to deal with AssertionError

 
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 10445
227
IntelliJ IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic