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
Handling Errors with Multiple Catch Blocks in C#
📖 Scenario: Imagine you are writing a simple calculator program that divides two numbers. Sometimes, users might enter zero as the divisor or enter invalid input. You want to handle these errors gracefully.
🎯 Goal: Build a C# program that uses try with multiple catch blocks to handle different types of errors separately.
📋 What You'll Learn
Create two integer variables named numerator and denominator with exact values.
Create a try block to perform division.
Add two catch blocks: one for DivideByZeroException and one for FormatException.
Print the result or error messages accordingly.
💡 Why This Matters
🌍 Real World
Handling errors gracefully is important in real-world programs to avoid crashes and give users helpful messages.
💼 Career
Understanding multiple catch blocks is essential for writing robust C# applications that handle different error types properly.
Progress0 / 4 steps
1
Create numerator and denominator variables
Create two integer variables called numerator and denominator with values 10 and 0 respectively.
C Sharp (C#)
Hint
Use int numerator = 10; and int denominator = 0; to create the variables.
2
Add a try block to divide numerator by denominator
Add a try block that attempts to divide numerator by denominator and stores the result in an integer variable called result.
C Sharp (C#)
Hint
Use try { int result = numerator / denominator; } to catch errors during division.
3
Add multiple catch blocks for DivideByZeroException and FormatException
Add two catch blocks after the try: one catching DivideByZeroException that prints "Cannot divide by zero.", and another catching FormatException that prints "Invalid input format.".
C Sharp (C#)
Hint
Use two separate catch blocks for each exception type with the exact messages.
4
Print the result if division succeeds
Inside the try block, after dividing, add a Console.WriteLine to print "Result: " followed by the result variable.
C Sharp (C#)
Hint
Use Console.WriteLine("Result: " + result); inside the try block.
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]