Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Error Handling Model Is More Robust?

I'm kind of torn between these two error-handling models:

  1. Create a boolean Error and a string ErrorMessage property for your object. Catch all exceptions internally in the object's methods and pass the messages along using conditional logic from the caller, ie:

    Dim o As New MyObject
    o.SomeMethod()
    If Not o.Error Then
        'Do stuff'
    Else
        Dim msg As String = o.ErrorMessage
        'do something with message'
    End If
    
  2. Throw exceptions in your object and handle them on the outside with Try Catch logic:

    Dim o As New MyObject
    Try
       o.SomeMethod()      
       'Do stuff'
    Catch ex As Exception
        Dim msg As String = ex.ErrorMessage
        'do something with message'
    End Try
    

To me, it seems like the same amount of code either way, except that you have property code for the Error and ErrorMessage properties. However, you also can tell when an error occurs without having to check for exceptions. Which pattern should I go with?

like image 728
Jason Avatar asked Sep 13 '09 18:09

Jason


3 Answers

I have decided to go with throwing exceptions instead of using error/return codes. I just recently looked really hard into this.

The #1 reason to throw exceptions is there is a possibility you can forget to check the error code. If you don't check it, then you will continue working while the error exists. With exceptions though, if you forget to handle them, then the exception will raise to the top and stop all processing. It is better for this to happen than to continue after unknown errors have occurred.

For more info check out the Exception chapter in Framework Design Guidelines: Conventions, Idioms, and Patterns for Reusable .NET Libraries, Second Edition by Addison-Wesley.

Joel Spolsky actually prefers error/return codes over exceptions but a lot of people disagree with him. Joel's post in favor of return codes can be found here. Check out this blog post and all of the comments with some good discussion regarding this subject.

like image 190
Dennis Avatar answered Sep 23 '22 04:09

Dennis


Prefer #2. For details, see this excerpt on Exception Throwing from the development of Microsoft's excellent Framework Design Guidelines, as Dennis mentioned. Note especially the section on Exceptions and Performance.

Short version:

Do not return error codes.

Do report execution failures by throwing exceptions.

Do not use exceptions for normal flow of control.

I highly recommend reading the book for a full discussion, complete with commentary from a number of the Microsoft luminaries.

like image 22
TrueWill Avatar answered Sep 25 '22 04:09

TrueWill


Exceptions should be used when something exceptional has happened.

e.g. you are passed a null (nothing) object when you expect one.

Uncle Bob recommends Exceptions over Error codes in his book Clean code.

He says

The problem with these [error codes] approaches is that they clutter the caller. The caller must check for errors immediately after the call. Unfortunately it's easy to forget. For this reason it is better to throw an exception when you encounter an error. The calling code is cleaner. Its logic is not obscured by error handling.

like image 29
Johnno Nolan Avatar answered Sep 25 '22 04:09

Johnno Nolan