Recall & Review
beginner
What is the purpose of multiple catch blocks in C#?
Multiple catch blocks allow a program to handle different types of exceptions separately, providing specific responses for each error type.
Click to reveal answer
beginner
How does C# decide which catch block to execute when an exception occurs?
C# checks each catch block in order and executes the first one that matches the type of the thrown exception or its base types.
Click to reveal answer
intermediate
Can you have a general catch block after specific ones? Why?
Yes, a general catch block (catch without specifying an exception type) can be placed last to catch any exceptions not handled by previous specific catch blocks.
Click to reveal answer
intermediate
What happens if no catch block matches the thrown exception?
If no catch block matches, the exception propagates up the call stack, potentially causing the program to terminate if unhandled.
Click to reveal answer
beginner
Show a simple example of multiple catch blocks in C#.
try {
int[] numbers = {1, 2, 3};
Console.WriteLine(numbers[5]);
} catch (IndexOutOfRangeException) {
Console.WriteLine("Index is out of range.");
} catch (Exception) {
Console.WriteLine("Some other error occurred.");
}
Click to reveal answer
What is the order of catch blocks when handling exceptions?
✗ Incorrect
Catch blocks should be ordered from most specific exceptions to most general to ensure the correct handler is executed.
What happens if a catch block for Exception is placed before a catch block for IndexOutOfRangeException?
✗ Incorrect
The general Exception catch block will catch all exceptions including IndexOutOfRangeException, so the specific catch block after it is unreachable.
Can you have multiple catch blocks for the same exception type?
✗ Incorrect
You cannot have multiple catch blocks for the same exception type in the same try statement.
What is the role of a catch block without an exception type?
✗ Incorrect
A catch block without an exception type acts as a general catch-all for any exceptions not previously caught.
If an exception is thrown but no catch block matches, what happens?
✗ Incorrect
If no catch block matches, the exception moves up to the calling method to be handled or cause program termination.
Explain how multiple catch blocks work in C# and why their order matters.
Think about how you would catch different problems separately and why catching general problems first might cause issues.
You got /4 concepts.
Write a simple C# try-catch example with at least two catch blocks for different exceptions.
Use exceptions like IndexOutOfRangeException and Exception for general errors.
You got /4 concepts.