I think the two main problems with my programs are my code structure/organization and my error handling. I'm reading Code Complete 2, but I need something to read for working with potential problems.

For example, on a website, if something can only happen if the user tampers with data via javascript, do you write for that? Also, when do you not catch errors? When you write a class that expects a string and an int as input, and they aren't a string and int, do you check for that, or do you let it bubble up to the calling method that passed incorrect parameters?

I know this is a broad topic that can't be answered in a single answer here, so what I'm looking for is a book or resource that's commonly accepted as teaching proper exception handling practice.

closed as off-topic by Thomas Owens May 15 '14 at 10:11

This question appears to be off-topic. The users who voted to close gave this specific reason:

  • "Questions asking us to recommend a tool, library or favorite off-site resource are off-topic for Programmers as they tend to attract opinionated answers and spam. Instead, describe the problem and what has been done so far to solve it." – Thomas Owens
If this question can be reworded to fit the rules in the help center, please edit the question.

  • 1
    what language and development platform are you using? – EL Yusubov Jun 26 '12 at 23:36
  • 1
    I think that "Clean code" has a good chapter about this problem – Hoàng Long Jun 27 '12 at 2:57
  • What @ElYusubov said. The implementation details and standards of the language/platform you are dealing with have a lot of sway over best practices. – vaughandroid Jun 27 '12 at 8:48

One of the good things to remember is perform exception handling when there is a need.

For .NET development platform just follow the MSDN guidelines - Best Practices for Handling Exceptions, as well as check this nice code-project article - Exception Handling Best Practices in .NET

However, the following guidelines are mostly true for any development platform:

  • Don't manage business logic with exceptions. Use conditional statements instead. If a control can be done with if-else statement clearly, don't use exceptions because it reduces readability and performance (e.g. null control, divide by zero control). .

  • Exception names must be clear and meaningful, stating the causes of exception.

  • Throw exceptions for error conditions while implementing a method. E.g. if you return -1, -2, -3 etc. values instead of FileNotFoundException, that method can not be understand.

  • Catch specific exceptions instead of the top Exception class. This will bring additional performance, readability and more specific exception handling.

  • Null control with conditionals is not an alternative to catching NullPointerException. If a method may return null, control it with if-else statement. If a return may throw NullPointerException, catch it.

  • Try not to re-throw the exception because of the price. Bu if re-throwing had been a must, re-throw the same exception instead of creating a new exception. This will bring additional performance. You may add additional info in each layer to that exception.

  • Define your own exception hierarchy by extending current Exception class (e.g. UserException, SystemException and their sub types) and use them. By doing this you can specialize your exceptions and define a reusable module/layer of exceptions. more to follow in this link

Not the answer you're looking for? Browse other questions tagged or ask your own question.