[jbosstools-dev] throw/catch Exception is evil

Max Rydahl Andersen max.andersen at redhat.com
Wed Jul 18 02:03:18 EDT 2007


Hi,

Just being trying to hunt down some issues I bumped into within the seam 
builder and views and I spent a looong time figuring out what was going on.

And eventually it boiled down to the over usage of just doing throw and 
catch Exception instead of actually trying to only handle the exceptions 
the code actually can handle.

So *please* stop doing things like

try {
    sp.store();
} catch (Exception e) {
     SeamCorePlugin.getPluginLog().logError("Error storing build results 
for " + sp.getProject().getName());
  }

that code should be:

try {
	sp.store();
    } catch (IOException e) {
	SeamCorePlugin.getPluginLog().logError("Error storing build results for 
" + sp.getProject().getName(), e);
		}

So what's the difference:

Catching Exception also catches RuntimeExceptions like 
NullPointerException meaning it swallows bugs! QA has zero change of 
catching such bugs if we hide it from them (plus all the exception 
handling code becomes useless when just using Exception).

Thus I changed the store() operation to only throw what it actually 
throws (IOException) plus actually logging the exception to be able to 
figure out why the IOException occurred.

Thanks ;)

/max



More information about the jbosstools-dev mailing list