Junior — Middle
Is it possible to set up multiple exception handlers within a single try block?
sobes.tech AI
Answer from AI
In C#, within a single try block, you can use multiple catch blocks to handle different types of exceptions. However, you cannot assign multiple handlers to the same try block in one catch — each catch handles exceptions of a specific type.
Example:
try
{
// code that may throw an exception
}
catch (ArgumentNullException ex)
{
// handle ArgumentNullException
}
catch (InvalidOperationException ex)
{
// handle InvalidOperationException
}
catch (Exception ex)
{
// general handler for all other exceptions
}
Thus, you can have multiple catch blocks, but each handles different exceptions, and they are executed sequentially after one try.