Jump into concepts and practice - no test required
or
Recommended
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
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?
AFrom most general to most specific exception types
BAlphabetical order by exception name
CRandom order
DFrom most specific to most general exception types
✗ 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?
AThe program will crash
BBoth catch blocks will execute
CThe IndexOutOfRangeException catch block will never be reached
DThe compiler will reorder the catch blocks automatically
✗ 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?
AYes, but only if they have different variable names
BNo, each exception type can only appear once in catch blocks
CYes, catch blocks can be duplicated freely
DOnly if they are inside different try blocks
✗ 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?
AIt catches any exception not caught by previous catch blocks
BIt catches only null reference exceptions
CIt is invalid syntax
DIt catches exceptions only in debug mode
✗ 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?
AThe exception propagates up the call stack
BThe program ignores the exception
CThe program automatically retries the operation
DThe exception is silently logged and discarded
✗ 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.
Practice
(1/5)
1.
What is the main purpose of using multiple catch blocks in C#?
easy
A. To avoid using try blocks
B. To handle different types of exceptions separately
C. To make the code run faster
D. To declare multiple variables
Solution
Step 1: Understand the role of catch blocks
catch blocks are used to handle errors that happen in the try block.
Step 2: Recognize why multiple catch blocks are used
Using multiple catch blocks lets you respond differently to different error types, making your program clearer and safer.
Final Answer:
To handle different types of exceptions separately -> Option B
Quick Check:
Multiple catch blocks = handle different exceptions [OK]
Hint: Multiple catch blocks handle different errors separately [OK]
Common Mistakes:
Thinking multiple catch blocks speed up code
Believing catch blocks replace try blocks
Using catch blocks to declare variables
2.
Which of the following is the correct syntax order for multiple catch blocks in C#?
The first catch block catches all Exception types, including FormatException.
Step 2: Identify unreachable catch block
Since Exception catch is first, the FormatException catch block can never run, causing a compile error.
Final Answer:
The FormatException catch block is unreachable -> Option A
Quick Check:
General catch before specific causes unreachable code [OK]
Hint: Place specific catch blocks before general ones to avoid unreachable code [OK]
Common Mistakes:
Putting general catch before specific
Ignoring unreachable code errors
Thinking finally block is mandatory
5.
You want to handle NullReferenceException and DivideByZeroException differently, but also catch any other exceptions generally. Which is the best order of catch blocks?
try {
// code that may throw exceptions
} catch (___) {
Console.WriteLine("Null reference error");
} catch (___) {
Console.WriteLine("Divide by zero error");
} catch (___) {
Console.WriteLine("Other error");
}
hard
A. Exception, DivideByZeroException, NullReferenceException
B. Exception, NullReferenceException, DivideByZeroException
C. DivideByZeroException, Exception, NullReferenceException
D. NullReferenceException, DivideByZeroException, Exception
Solution
Step 1: Identify specific exceptions
NullReferenceException and DivideByZeroException are specific exceptions to catch first.
Step 2: Place general exception last
The general Exception catch block should come last to catch all other exceptions.
Final Answer:
NullReferenceException, DivideByZeroException, Exception -> Option D
Quick Check:
Specific exceptions first, general last [OK]
Hint: Catch specific exceptions before general Exception last [OK]