#851 – A General Philosophy for Handling Exceptions

An exception is an event that occurs at run-time and is not part of the normal expected execution of your code.  Exceptions can arise because of user errors, runtime errors, or bugs in your application.

You can include code in your application to catch and handle exceptions.  Your exception handling code will intercept exceptions that occur and you will then have a chance to react to the exception that is occurring.

As a general guideline for handling exceptions, you should:

  • Handle any exceptions that you can do something about.
    • E.g. Catch exceptions that indicate that the user entered malformed input and inform the user of what went wrong.
  • Include a top-level handler for unhandled exceptions
    • In this handler, inform the user that an unrecoverable error occurred.
    • Optionally, log information about the exception, to help diagnose what went wrong
Advertisement

About Sean
Software developer in the Twin Cities area, passionate about software development and sailing.

3 Responses to #851 – A General Philosophy for Handling Exceptions

  1. Pingback: Dew Drop – May 24, 2013 (#1,554) | Alvin Ashcraft's Morning Dew

  2. Dirk Strauss says:

    This is a great post. Few implement it correctly though, especially when creating custom exceptions. I found this great article on creating custom exceptions. Take a look: http://blogs.msdn.com/b/agileer/archive/2013/05/17/the-right-way-to-write-a-custom-exception-class.aspx

  3. Pingback: #884 – Things You Might Do in an Exception Handler | 2,000 Things You Should Know About C#

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

%d bloggers like this: