Skip to content

How to Catch Multiple Exceptions C#

How to catch multiple Exceptions C#
Become a developer with our complete learning paths
Become a developer with our complete learning paths

This article explores different approaches to handling multiple exceptions in C#. You will learn to share a single catch block across multiple exception types or write separate ones for each exception type.

In this article, we’ll cover:

  • Caching multiple exceptions in multiple blocks
  • Priority of catch blocks
  • The role of catch blocks with no exceptions
  • Catching multiple exceptions in a single block
  • Sharing catch blocks between multiple exception types

 

Multiple Catch Blocks

The simplest form of catching exceptions in multiple blocks is to write a separate catch block for each possible exception type that the try block may throw:

 

In this example, the code inside the try block attempts to parse a string to an integer, which can throw a FormatException if the string is not in the correct format or an OverflowException if the parsed value is too large or too small. The catch blocks handle these two exceptions separately by printing an error message. 

If you want to boost your C# career, check out our powerful ASP.NET full-stack web development course that also covers test-driven development and C# software architecture.

 

 

Execution Priority

It is important to note that when catching multiple exceptions, the order in which the exception types are listed in the catch block is important. The catch block for the most specific exception type should come first, followed by catch blocks for more general exception types.

Consider the following example:

 

If you have a catch block for a specific exception type, such as ArgumentNullException, followed by a catch block for the more general exception type, ArgumentException, the code in the ArgumentNullException catch block will be executed if an ArgumentNullException is thrown. 

In contrast, if another exception type that derives from the ArgumentException is thrown in the try block, the code in the ArgumentException catch block will be executed.

 

 

Catch Blocks Without Exception Types

You can also use the catch block without any exception type specified. This catch block will catch all the exceptions that are not handled by the catch blocks specified before it.

 

Catch only those exceptions which you can handle. When you catch an exception, it is assumed that you have taken care of it.

 

 

Pattern Matching

In C# 7.0 and later versions, you can use pattern matching in exception handling to match the exception type and extract information from it

The is keyword can be used in a catch block to match the exception type and extract information from it. The following is an example of using pattern matching in a catch block:

 

In this example, the catch block uses the is keyword to match the exception type and check whether it is a FormatException or an OverflowException. If the exception is one of these types, the code inside the catch block is executed.

Additionally, you can extract information from the exception using the when keyword and a boolean expression like this:

 

It is important to note that the use of pattern matching in exception handling can make the code more readable and maintainable. However, it should be used judiciously and only when it makes sense to do so. By the way, did you know that we offer a unique and powerful online course that boosts your C# career? Check it out here!

 

 

Conclusion

It is essential to order the catch blocks correctly and only to catch exceptions you can handle. Catching multiple exceptions in C# allows you to handle different exceptions in a single code block. Handling multiple exception types in a single block allows you to share the common exception-handling logic between multiple exception types that may occur on the same try block. 

Lost in coding? Discover our Learning Paths!
Lost in coding? Discover our Learning Paths!
Enter your email and we will send you the PDF guide:
Enter your email and we will send you the PDF guide