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
Throw and Rethrow Patterns
📖 Scenario: Imagine you are building a simple calculator app that divides two numbers. Sometimes, the user might enter zero as the divisor, which causes an error. You want to catch this error, add a message, and then rethrow it so the program can handle it properly.
🎯 Goal: You will create a program that demonstrates how to throw and rethrow exceptions in C#. This helps you understand how to handle errors and pass them along with extra information.
📋 What You'll Learn
Create a method called Divide that takes two integers numerator and denominator.
Inside Divide, throw a DivideByZeroException if denominator is zero.
Use a try-catch block in Divide to catch the exception, add a message, and rethrow it.
In Main, call Divide and catch the rethrown exception to print its message.
💡 Why This Matters
🌍 Real World
Throwing and rethrowing exceptions is common in real apps to handle errors clearly and pass useful messages up the call chain.
💼 Career
Understanding exception handling is essential for writing robust, maintainable code in professional software development.
Progress0 / 4 steps
1
Create the Divide method with parameters
Create a method called Divide that takes two integer parameters named numerator and denominator. For now, just return 0 inside the method.
C Sharp (C#)
Hint
Define a method named Divide with two int parameters and return an int value.
2
Throw DivideByZeroException if denominator is zero
Inside the Divide method, add an if statement to check if denominator is zero. If it is, throw a new DivideByZeroException with the message "Cannot divide by zero."
C Sharp (C#)
Hint
Use throw new DivideByZeroException with the exact message inside the if block.
3
Catch and rethrow the exception with a message
Modify the Divide method to use a try-catch block. Put the if check and division inside the try. In the catch (DivideByZeroException ex), throw a new DivideByZeroException with the message "Error in Divide method: " plus the original exception message.
C Sharp (C#)
Hint
Use try-catch to catch the exception and rethrow with a new message combining your text and ex.Message.
4
Call Divide in Main and print the exception message
In the Main method, call Divide(10, 0) inside a try-catch block. Catch DivideByZeroException ex and print ex.Message using Console.WriteLine.
C Sharp (C#)
Hint
Use try-catch in Main to call Divide and print the exception message.
Practice
(1/5)
1. What does the throw statement do in C# exception handling?
easy
A. It logs the error without stopping the program.
B. It ignores the error and continues execution.
C. It stops the program and signals an error.
D. It automatically fixes the error and resumes.
Solution
Step 1: Understand the purpose of throw
The throw statement is used to signal that an error has occurred and to stop normal program flow.
Step 2: Recognize program behavior on throw
When throw is executed, the program stops and looks for a matching catch block or terminates if none is found.
Final Answer:
It stops the program and signals an error. -> Option C
Quick Check:
throw stops program = C [OK]
Hint: Throw always stops execution and signals an error [OK]
Common Mistakes:
Thinking throw continues program normally
Confusing throw with logging
Assuming throw fixes errors automatically
2. Which of the following is the correct syntax to rethrow an exception in a catch block without changing it?
easy
A. throw catch;
B. throw ex;
C. throw new Exception();
D. throw;
Solution
Step 1: Identify rethrow syntax
To rethrow the caught exception preserving the original stack trace, use throw; without specifying the exception variable.
Step 2: Understand difference from throw ex;
throw ex; resets the stack trace, which is not a pure rethrow.
Final Answer:
throw; -> Option D
Quick Check:
Rethrow syntax = throw; [OK]
Hint: Use plain 'throw;' to rethrow without losing stack trace [OK]
Common Mistakes:
Using 'throw ex;' which resets stack trace
Trying to throw a new exception instead
Using invalid syntax like 'throw catch;'
3. What will be the output of the following C# code?