MaxStocker.com   MaxStocker.com    
   
Home About Blog Stuff Contact
 
   
 

May 2009

The other day...
Posted : Thu May 28th

Added Java to Category List
Posted : Tue May 26th

Proper exception handling can't wait
Posted : Mon May 25th

Connection woes
Posted : Sat May 23rd

Why I hate Apple
Posted : Wed May 20th

Extracting DOCX content with Java
Posted : Tue May 19th

I notice
Posted : Fri May 15th

JDBC Best Practices
Posted : Fri May 15th

Simple PHP RSS loader
Posted : Sun May 10th

MySQL and DBCP for Tomcat 5
Posted : Sat May 9th

Tracing 316.70.50.1
Posted : Mon May 4th

The flip side
Posted : Tue April 28th

Starting to irritate me
Posted : Fri April 24th

As seen on the internet
Posted : Wed April 22nd

Recent Comments

Max in Whose blog is it anyway?
on Mon May 10th

Rob in Whose blog is it anyway?
on Fri May 7th

Anonymous in SEO and the magic beans
on Thu April 8th

Max in SEO and the magic beans
on Thu April 8th

n.o. in SEO and the magic beans
on Thu April 8th

silky in Right way, wrong way
on Fri February 19th

Categories

Technical
68 Entries

Security
18 Entries

Java
23 Entries

Privacy
6 Entries

Database
11 Entries

Internet
56 Entries

Business
31 Entries

Site Updates
19 Entries

Personal
85 Entries

RSS Feed RSS Feed

Tag Cloud

Proper exception handling can't wait
Posted : Monday May 25th, 2009

The most overall common mistake I see in Java code posted in forums and discussion groups is bad exception handling. By bad exception handling I mean seeing the following.


try{
  //some code here
}catch(SomeException se){
}

In other words, no actual handling of the exception, no printing of the stack trace. Nothing.

When told to fix this it's not uncommon to get back responses like

I know the exception handling is bad but I'm going to fix it later

I find it very difficult to understand the sort of errors in logic that can lead one to make such a statement. But let's clear this up. No. You don't add "better" or "fixed" exception handling later, you add it now.

The fact is that the sort of nonsense posted above is not just useless but actually hides problems from you, and during development and testing, well, you really do want to know about problems. An empty catch block is just laziness but worse a sign of some stupidity because swallowing exceptions only means that you will have no real clue what is working and what is not.

Does that mean you have to do "everything" that you will eventually do in your catch block from the get-go? No. It's okay to improve exception handling later, throwing up dialogs to the user, logging the exception, whatever. But at the very minimum a catch block must always have a call to printStackTrace. That will at least tell you, while you are developing and testing, that a problem actually happened and where it happened.

And if you don't feel like doing that? Then quite simply don't catch the exception at all. Simply add a throws clause to the method and let the exception be propagated to the caller, etc. Quite frankly you can throw right back out of main during testing if you like. It's not perfect but again at least you get a stack trace.

But whatever you do, don't swallow exceptions. Ever. I don't care that you're going to "fix" it later or whatever other lame excuse you have, you simply cannot debug code that swallows exceptions so just don't.

Tags

code  exception  handling 

Categories

Technical  Java 

Comments

 
   
  Follow me on Twitter   My Facebook Profile   My LinkedIn Profile   RSS feed of my blog Home   |   About   |   Blog   |   Stuff   |   Contact   |   Privacy Policy  
   
  © 2008 Max Stocker