Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Coding Crimes: Ignoring Exceptions

DZone's Guide to

Coding Crimes: Ignoring Exceptions

· Java Zone
Free Resource

Are you joining the containers revolution? Start leveraging container management using Platform9's ultimate guide to Kubernetes deployment.

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. 

Moving towards a private or Hybrid cloud infrastructure model? Get started with our OpenStack Deployment Models guide to learn the proper deployment model for your organization.

Topics:

Opinions expressed by DZone contributors are their own.

THE DZONE NEWSLETTER

Dev Resources & Solutions Straight to Your Inbox

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

X

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

{{ parent.tldr }}

{{ parent.urlSource.name }}