Over a million developers have joined DZone.

Coding Crimes: Ignoring Exceptions

· Java Zone

Learn more about Kotlin, a new programming language designed to solve problems that software developers face every day brought to you in partnership with JetBrains.

Templates in IDEs such as Eclipse and NetBeans are a great feature. In particular, quick fix items such as "surround with try/catch" can save a lot of typing time. But, as with all things, the power given to the developer can be mis-used. This has got to be one of the worst coding crimes. 

Depending on your IDE and the templates you use, you'll probably get this: 

try 
{

} catch (Exception e)
{
// TODO: handle exception
}

Or, maybe you'll at least get the exception written out to console.

try 
{

} catch (IOException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}

For a start, the exception should be logged at the very least, not just written out to the console.  Also, in most cases, the exception should be thrown back to the caller for them to deal with. If it doesn't need to be thrown back to the caller, then the exception should be handled. And some comments would be nice too. 

The usual excuse for this type of code is "I didn't have time", but there is a ripple effect when code is left in this state. Chances are that most of this type of code will never get out in the final production. Code reviews or static analysis tools should catch this error pattern. But that's no excuse, all this does is add time to the maintainance and debugging of the software.  This also makes you question TODOs, and brings me back to my previous thoughts on writing the best code from the very start. 

The Java Zone is brought to you in partnership with JetBrains.  Discover how powerful static code analysis and ergonomic design make development not only productive but also an enjoyable experience.

Topics:

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}