In C#, you can catch two or more different exceptions in the same catch block by using a multi-catch statement introduced in C# 6.0. This feature allows you to handle different types of exceptions with a single catch block, reducing code duplication and making your exception handling more concise.
Here's how you can catch multiple exceptions in the same catch block:
try { // Code that might throw exceptions } catch (ExceptionType1 ex1) // Catch the first exception type { // Handle ExceptionType1 } catch (ExceptionType2 ex2) // Catch the second exception type { // Handle ExceptionType2 } // Add more catch blocks for additional exception types if needed
In the code above, replace ExceptionType1
and ExceptionType2
with the actual types of exceptions you want to catch. You can add more catch blocks for additional exception types as necessary.
Here's an example that demonstrates catching two different exception types:
using System; class Program { static void Main() { try { int x = 10; int y = 0; int result = x / y; // This will throw a DivideByZeroException // Uncomment the line below to test the IndexOutOfRangeException // int[] arr = new int[3]; // int value = arr[5]; // This will throw an IndexOutOfRangeException } catch (DivideByZeroException ex) { Console.WriteLine("DivideByZeroException occurred: " + ex.Message); } catch (IndexOutOfRangeException ex) { Console.WriteLine("IndexOutOfRangeException occurred: " + ex.Message); } catch (Exception ex) { // This catch block will catch any other exception types not caught by the previous catch blocks Console.WriteLine("An exception occurred: " + ex.Message); } } }
In this example, we catch the DivideByZeroException
and IndexOutOfRangeException
in separate catch blocks. Any other exception that is not explicitly caught by the previous catch blocks will be caught by the last catch (Exception ex)
block.
Remember that it's essential to catch specific exception types before catching more general exception types. The catch blocks are evaluated in order, and the first matching catch block will be executed. If you catch a more general exception type before a specific one, the specific catch block will never be reached.
"C# catch multiple exceptions in one catch block"
try { // Code that may throw exceptions } catch (ExceptionType1 ex1) // Catch the first exception type { Console.WriteLine("ExceptionType1 caught: " + ex1.Message); } catch (ExceptionType2 ex2) // Catch the second exception type { Console.WriteLine("ExceptionType2 caught: " + ex2.Message); } catch (Exception ex) // Catch any other exceptions { Console.WriteLine("Other exception caught: " + ex.Message); }
Description: This code catches two specific exception types (ExceptionType1
and ExceptionType2
) in separate catch blocks within a single try-catch structure.
"C# catch multiple exceptions with common handling"
try { // Code that may throw exceptions } catch (ExceptionType1 | ExceptionType2 ex) // Catch both exception types with common handling { Console.WriteLine("ExceptionType1 or ExceptionType2 caught: " + ex.Message); } catch (Exception ex) // Catch any other exceptions { Console.WriteLine("Other exception caught: " + ex.Message); }
Description: This code catches both ExceptionType1
and ExceptionType2
with common handling in a single catch block using the pipe (|
) syntax.
"C# catch two exceptions and rethrow others"
try { // Code that may throw exceptions } catch (ExceptionType1 ex1) // Catch the first exception type { Console.WriteLine("ExceptionType1 caught: " + ex1.Message); } catch (ExceptionType2 ex2) // Catch the second exception type { Console.WriteLine("ExceptionType2 caught: " + ex2.Message); } catch // Rethrow other exceptions after handling { throw; }
Description: This code catches two specific exception types (ExceptionType1
and ExceptionType2
) and rethrows any other exceptions after handling.
"C# catch multiple exceptions using when"
try { // Code that may throw exceptions } catch (Exception ex) when (ex is ExceptionType1 || ex is ExceptionType2) // Catch specific exceptions with "when" clause { Console.WriteLine("ExceptionType1 or ExceptionType2 caught: " + ex.Message); } catch (Exception ex) // Catch any other exceptions { Console.WriteLine("Other exception caught: " + ex.Message); }
Description: This code catches specific exception types (ExceptionType1
or ExceptionType2
) with additional conditions using the "when" clause.
"C# catch two exceptions with specific conditions"
try { // Code that may throw exceptions } catch (ExceptionType1 ex1) when (ex1.Property == "Condition1") // Catch the first exception type with condition { Console.WriteLine("ExceptionType1 caught with Condition1: " + ex1.Message); } catch (ExceptionType2 ex2) when (ex2.Property == "Condition2") // Catch the second exception type with condition { Console.WriteLine("ExceptionType2 caught with Condition2: " + ex2.Message); } catch (Exception ex) // Catch any other exceptions { Console.WriteLine("Other exception caught: " + ex.Message); }
Description: This code catches two specific exception types (ExceptionType1
and ExceptionType2
) with additional conditions using the "when" clause.
"C# catch two exceptions and log others"
try { // Code that may throw exceptions } catch (ExceptionType1 ex1) // Catch the first exception type { Console.WriteLine("ExceptionType1 caught: " + ex1.Message); } catch (ExceptionType2 ex2) // Catch the second exception type { Console.WriteLine("ExceptionType2 caught: " + ex2.Message); } catch (Exception ex) // Log any other exceptions { Logger.LogException(ex); }
Description: This code catches two specific exception types (ExceptionType1
and ExceptionType2
) and logs any other exceptions using a catch-all block.
"C# catch two exceptions and retry"
const int maxRetries = 3; int retries = 0; while (retries < maxRetries) { try { // Code that may throw exceptions } catch (ExceptionType1 ex1) // Catch the first exception type { Console.WriteLine("ExceptionType1 caught: " + ex1.Message); retries++; } catch (ExceptionType2 ex2) // Catch the second exception type { Console.WriteLine("ExceptionType2 caught: " + ex2.Message); retries++; } catch (Exception ex) // Retry for any other exceptions { Console.WriteLine("Other exception caught, retrying: " + ex.Message); retries++; } }
Description: This code catches two specific exception types (ExceptionType1
and ExceptionType2
) and implements a retry mechanism for any other exceptions.
"C# catch two exceptions and perform cleanup"
try { // Code that may throw exceptions } catch (ExceptionType1 ex1) // Catch the first exception type { Console.WriteLine("ExceptionType1 caught: " + ex1.Message); // Perform cleanup for ExceptionType1 } catch (ExceptionType2 ex2) // Catch the second exception type { Console.WriteLine("ExceptionType2 caught: " + ex2.Message); // Perform cleanup for ExceptionType2 } catch (Exception ex) // Perform cleanup for any other exceptions { Console.WriteLine("Other exception caught, performing cleanup: " + ex.Message); // Perform cleanup for other exceptions }
Description: This code catches two specific exception types (ExceptionType1
and ExceptionType2
) and performs cleanup operations for each.
"C# catch two exceptions and handle aggregate exception"
try { // Code that may throw exceptions } catch (ExceptionType1 ex1) // Catch the first exception type { Console.WriteLine("ExceptionType1 caught: " + ex1.Message); } catch (ExceptionType2 ex2) // Catch the second exception type { Console.WriteLine("ExceptionType2 caught: " + ex2.Message); } catch (AggregateException aggregateEx) // Handle as an aggregate exception { foreach (Exception innerEx in aggregateEx.InnerExceptions) { Console.WriteLine("Aggregate Exception caught: " + innerEx.Message); } }
Description: This code catches two specific exception types (ExceptionType1
and ExceptionType2
) and handles them as an aggregate exception.
"C# catch two exceptions and differentiate"
try { // Code that may throw exceptions } catch (ExceptionType1 ex1) // Catch the first exception type { Console.WriteLine("ExceptionType1 caught: " + ex1.Message); // Handle ExceptionType1 specific logic } catch (ExceptionType2 ex2) // Catch the second exception type { Console.WriteLine("ExceptionType2 caught: " + ex2.Message); // Handle ExceptionType2 specific logic } catch (Exception ex) // Catch any other exceptions { Console.WriteLine("Other exception caught: " + ex.Message); }
Description: This code catches two specific exception types (ExceptionType1
and ExceptionType2
) and differentiates between them, allowing for specific logic for each exception type.
directory imagemagick video.js access-denied jql do-while tuples selenium-chromedriver formbuilder decision-tree