#889 – Catching Exceptions that Derive from a Common Base Type

When you specify an exception type to catch in a handler, the handler will catch all exceptions whose type matches the specified type.  It will also catch exceptions where the exception object’s type is a type that derives from the specified type.

In the example below, we catch exceptions of type System.ArgumentException.  This handler will catch exceptions where the exception object’s type is:

  • ArgumentException
  • ArgumentNullException  (derives from ArgumentException)
  • ArgumentOutOfRangeException  (derives from ArgumentException)
  • Any custom exception types you define that derive from ArgumentException or from one of its child classes
  • Various other exception types in the Framework that derive from ArgumentException
                Dog d = new Dog("Bob", 3);
            catch (ArgumentException exc)
                Console.WriteLine("You've got a problem with an argument");

About Sean
Software developer in the Twin Cities area, passionate about .NET technologies. Equally passionate about my own personal projects related to family history and preservation of family stories and photos.

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 )

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s

%d bloggers like this: