Challenge - 5 Problems
Try-Catch Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of try-catch with exception thrown
What is the output of this C# code?
C Sharp (C#)
try { Console.WriteLine("Start"); throw new Exception("Error"); Console.WriteLine("End"); } catch (Exception ex) { Console.WriteLine("Caught: " + ex.Message); } Console.WriteLine("After try-catch");
Attempts:
2 left
💡 Hint
Remember that code after the throw inside try is skipped.
✗ Incorrect
The code prints "Start", then throws an exception. The line "End" is skipped. The catch block prints the exception message. Finally, the line after try-catch runs.
❓ Predict Output
intermediate2:00remaining
Output when no exception is thrown
What will this C# code print?
C Sharp (C#)
try { Console.WriteLine("Try block"); } catch { Console.WriteLine("Catch block"); } finally { Console.WriteLine("Finally block"); }
Attempts:
2 left
💡 Hint
No exception means catch block is skipped.
✗ Incorrect
Since no exception occurs, the catch block is skipped. The finally block always runs.
❓ Predict Output
advanced2:00remaining
Exception in finally block
What is the output of this code?
C Sharp (C#)
try { Console.WriteLine("In try"); throw new Exception("Try exception"); } catch (Exception ex) { Console.WriteLine("In catch: " + ex.Message); } finally { Console.WriteLine("In finally"); throw new Exception("Finally exception"); } Console.WriteLine("After try-catch-finally");
Attempts:
2 left
💡 Hint
Exception in finally overrides previous exception.
✗ Incorrect
The try throws an exception caught by catch. Finally runs and throws a new exception, which is unhandled and stops execution.
❓ Predict Output
advanced2:00remaining
Return value with try-catch-finally
What value does this method return?
C Sharp (C#)
int Test() { try { return 1; } catch { return 2; } finally { return 3; } } Console.WriteLine(Test());
Attempts:
2 left
💡 Hint
Finally return overrides other returns.
✗ Incorrect
The finally block's return overrides the try's return, so the method returns 3.
🧠 Conceptual
expert2:00remaining
Exception propagation with nested try-catch
Consider this code snippet. Which statement is true about the output?
C Sharp (C#)
try { try { throw new InvalidOperationException("Inner"); } catch (ArgumentException) { Console.WriteLine("Caught ArgumentException"); } } catch (Exception ex) { Console.WriteLine("Caught Exception: " + ex.Message); }
Attempts:
2 left
💡 Hint
Inner catch only handles ArgumentException, not InvalidOperationException.
✗ Incorrect
The inner catch does not catch InvalidOperationException, so it propagates to outer catch which prints the message.