0
0
C Sharp (C#)programming~20 mins

Throw and rethrow patterns in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Throw and Rethrow Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of rethrowing an exception with throw;
What will be the output of this C# program when an exception is rethrown using throw; inside the catch block?
C Sharp (C#)
using System;
class Program {
    static void Main() {
        try {
            ThrowException();
        } catch (Exception ex) {
            Console.WriteLine("Caught: " + ex.Message);
            try {
                throw;
            } catch (Exception e) {
                Console.WriteLine("Rethrown caught: " + e.Message);
            }
        }
    }
    static void ThrowException() {
        throw new InvalidOperationException("Original error");
    }
}
A
Caught: Original error
Rethrown caught: Original error
B
Caught: Original error
Rethrown caught: Exception of type 'System.Exception' was thrown.
C
Caught: Original error
Rethrown caught: System.InvalidOperationException
D
Caught: Original error
Rethrown caught: System.InvalidOperationException: Original error
Attempts:
2 left
💡 Hint
Remember that using throw; preserves the original exception and its message.
Predict Output
intermediate
2:00remaining
Effect of throw ex; vs throw; on stack trace
Consider this code snippet. What will be the output regarding the exception message and stack trace when using throw ex; instead of throw; inside the catch block?
C Sharp (C#)
using System;
class Program {
    static void Main() {
        try {
            ThrowException();
        } catch (Exception ex) {
            Console.WriteLine("Caught: " + ex.Message);
            try {
                throw ex; // Change this line to 'throw;' to compare
            } catch (Exception e) {
                Console.WriteLine("Rethrown caught: " + e.Message);
                Console.WriteLine(e.StackTrace.Split('\n')[0]);
            }
        }
    }
    static void ThrowException() {
        throw new InvalidOperationException("Original error");
    }
}
A
Caught: Original error
Rethrown caught: Original error
 at Program.Main() ...
B
Caught: Original error
Rethrown caught: Exception of type 'System.Exception' was thrown.
C
Caught: Original error
Rethrown caught: Original error
 at Program.ThrowException() ...
D
Caught: Original error
Rethrown caught: Original error
 at Program.Main() ... (stack trace reset)
Attempts:
2 left
💡 Hint
Using throw ex; resets the stack trace to the current throw point.
🔧 Debug
advanced
2:00remaining
Identify the problem with rethrowing exception
This code tries to catch and rethrow an exception but causes unexpected behavior. What is the main problem?
C Sharp (C#)
try {
    DoWork();
} catch (Exception ex) {
    Log(ex.Message);
    throw ex;
}

void DoWork() {
    throw new Exception("Error in DoWork");
}

void Log(string message) {
    Console.WriteLine("Log: " + message);
}
AThe Log method causes a runtime error.
BThe catch block does not catch the exception properly.
CUsing 'throw ex;' resets the stack trace, losing original error location.
DThe exception message is lost after rethrowing.
Attempts:
2 left
💡 Hint
Consider how 'throw ex;' affects the stack trace compared to 'throw;'.
🧠 Conceptual
advanced
2:00remaining
Why prefer 'throw;' over 'throw ex;' when rethrowing?
Which of the following best explains why using throw; is preferred over throw ex; when rethrowing an exception in C#?
AThere is no difference; both behave identically.
B<code>throw;</code> preserves the original stack trace, aiding debugging, while <code>throw ex;</code> resets it.
C<code>throw;</code> creates a new exception object, while <code>throw ex;</code> reuses the old one.
D<code>throw ex;</code> preserves the original stack trace, while <code>throw;</code> resets it.
Attempts:
2 left
💡 Hint
Think about what happens to the stack trace in each case.
Predict Output
expert
2:00remaining
Output when rethrowing inside nested try-catch
What is the output of this C# program that uses nested try-catch blocks and rethrows exceptions?
C Sharp (C#)
using System;
class Program {
    static void Main() {
        try {
            try {
                throw new Exception("Inner exception");
            } catch (Exception ex) {
                Console.WriteLine("Caught inner: " + ex.Message);
                throw;
            }
        } catch (Exception ex) {
            Console.WriteLine("Caught outer: " + ex.Message);
        }
    }
}
A
Caught inner: Inner exception
Caught outer: Inner exception
BCaught inner: Inner exception
CCaught outer: Inner exception
DNo output, program crashes
Attempts:
2 left
💡 Hint
The inner catch rethrows the exception to the outer catch.