views:

250

answers:

10

At what point during development do you typically implement your exception handlers? Do you write them at the same time as you write the surrounding code, or do you write your code and then come back to "harden" it later?

I typically do the latter so that I can see exactly where and how my code fails, but I worry that my code isn't as resilient as it could be if I would have written the exception handlers right away.

At the same time, I don't want to spend a whole bunch of development time figuring out all the possible ways that my code could fail when there are other implementation details that I haven't settled on yet.

I'm curious as to how other developers do this.

Update: I just wanted to thank everyone for their answers!

A: 

during development, when:

  • a unit test require it
  • when some presentation/persistence code require it

EDIT

in Java sometimes, you must take care error handling at very early stage (checked exceptions) and sometimes this is very annoying.

dfa
A: 

Sometimes both. In some cases I know of the exceptions that can be thrown and I want to handle as I'm writing the code, and so I write the handlers right then and there. Other times I don't know of all of the exceptions and find them later, either through documentation, testing or both.

jasonh
A: 

It's a combination of both. There are things that I know can go wrong like database connections, configuration settings, file read/writes as well as the red flags from the functional/tech specifications. I try to setup the try/catch for those ASAP.

As the application gets bigger over time I then start to see patterns and trends with either how the user is using the application and or how me and or the team has developed it and add those try/catches as needed.

Marc
A: 

It kind of depends on the nature of the project you are working on. In my case, if I'm familiar with the logic of the system, I should know where, and how, to handle exceptions even before writing code. On the other hand, I would write my stuff, test it and then write the handlers.

nairdaen
A: 

If I am calling an API then I look at what exceptions can be thrown and decide based on the list. The exceptions that can be thrown generally fall into categories:

  • Improbable in my view this will get thrown - make sure that code fails nicely
  • Realistic that this will get thrown - what should I do if this gets called?
  • Certain that this will get thrown based on current inputs - add validation to inputs to stop it getting thrown
  • Could I raise a more relevant exception? - if an exception is likely to get to get called would it be clearer for other calling code if I raised a new/different exception

In general I think it is always good practice to have catch all try catch blocks high up in the call stack that can catch general exceptions (Throwable) and then report these nicely to the user - perhaps with an interface that will then email the error and stacktrace to the development team and ask for user comments.

Grouchal
A: 

In my exception handler I usually raise a higher-level exception. For example, when parsing a file in Python, some string, list and dict operations may raise ValueError, IndexError or KeyError. These exceptions are usually not helpful for the caller, so I write an exception handler, which raises a descriptive MyParseError. I do this at the same time when writing the method, but later, when writing the test, I sometimes make the exception message more verbose.

pts
+6  A: 

I either write the exception handlers immediately, or I allow the exceptions to propagate upwards. I'm a big fan of what I call the "You're Not Going To Go Back And Fix It Later, Are You?" principle. You say you will, but honestly, once you get it working, you're not going to go back and fix it later, are you? Just get it right right now! Write your exception handlers right away, or add a throws clause and make it somebody else's problem. Do the right thing right now.

But you know what, sometimes you can't. Whatever you do, do not swallow exceptions with empty exception handlers! This is evil:

try {
    connection.close();
}
catch (Exception e) {
    // TODO Auto-generated code
}

I drop kick anybody on my team who checks that in.

If you really don't know what to do about an exception and cannot add a throws clause to propagate it upwards, at least do something halfway responsible. Print a stack trace, if nothing else. It's not ideal but at least you're not hiding errors.

catch (IOException exception) {
    exception.printStackTrace();
}

Logging it via your application's logging system is better, though you shouldn't make a habit of it. It's supposed to be the caller's responsibility to handle these kinds of things.

catch (IOException exception) {
    log.error(exception, "Unable to open configuration file %s.", fileName);
}

As a last resort, you can do an end run around your throws clause by wrapping your exception in a RuntimeException. At least you're giving somebody higher up the call chain a chance to handle the error, which is normally the Right Thing To Do.

catch (IOException exception) {
    throw new RuntimeException(exception);
}
John Kugelman
A: 

My approach is to address exception handling immediately, since it's not some aimless burden that you can happily postpone.

Just handle the exceptions that apply at the point that you write your code, propagate all those that do not matter, and only come back later to fix whatever is broken, saves you a lot of tears.

BakerTheHacker
A: 

As a rule, not only do I write my exception handling when I'm writing the code, but I try to write the code to avoid exceptions in the first place. The advantages are that if I know I need to handle an exception I remember to and if I can avoid an exception that is always a plus. I also test my code after I've written it using boundary conditions to see if there's any possible exceptions that I may have missed.

indyK1ng
Be careful, some exceptions just can't be avoided. I've thought myself smart in the past for doing just that and been proven wrong.
CurtainDog
I know that some exceptions can't be avoided but the ones I know can be avoided, I avoid either by adding a small if statement to keep the program from proceeding or through some other method.
indyK1ng
A: 

Writing the handlers when you are writing the actual code is the best habbit i guess because you are very clear of the failures that may occur although you can add others when you discover it.

handling the exception may be tedious for the first time but it would save lot of time while debugging for some error i.e support.

Vinay Pandey