Bird
Raised Fist0
C Sharp (C#)programming~20 mins

Try-catch execution flow in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style10 modes available

Start learning this pattern below

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
Challenge - 5 Problems
🎖️
Try-Catch Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
AStart\nCaught: Error\nAfter try-catch
BStart\nEnd\nCaught: Error\nAfter try-catch
CCaught: Error\nAfter try-catch
DStart\nAfter try-catch
Attempts:
2 left
💡 Hint
Remember that code after the throw inside try is skipped.
Predict Output
intermediate
2: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");
}
ACatch block\nFinally block
BTry block\nFinally block
CTry block\nCatch block\nFinally block
DFinally block
Attempts:
2 left
💡 Hint
No exception means catch block is skipped.
Predict Output
advanced
2: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");
AIn try\nIn finally\nUnhandled exception: Finally exception
BIn try\nIn catch: Try exception\nIn finally\nAfter try-catch-finally
CIn try\nIn catch: Try exception\nIn finally\nUnhandled exception: Finally exception
DIn try\nIn catch: Try exception\nAfter try-catch-finally
Attempts:
2 left
💡 Hint
Exception in finally overrides previous exception.
Predict Output
advanced
2: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());
A3
B2
C1
D0
Attempts:
2 left
💡 Hint
Finally return overrides other returns.
🧠 Conceptual
expert
2: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);
}
ACaught ArgumentException
BCaught Exception: ArgumentException
CNo output, program crashes
DCaught Exception: Inner
Attempts:
2 left
💡 Hint
Inner catch only handles ArgumentException, not InvalidOperationException.

Practice

(1/5)
1. What happens when an exception occurs inside a try block in C#?
easy
A. The program immediately jumps to the catch block to handle the error.
B. The program ignores the error and continues running the try block.
C. The program stops running without executing any further code.
D. The program restarts the try block from the beginning.

Solution

  1. Step 1: Understand try-catch flow

    The try block runs code that might cause an error.
  2. Step 2: Exception triggers catch block

    If an error happens, control moves to the catch block to handle it.
  3. Final Answer:

    The program immediately jumps to the catch block to handle the error. -> Option A
  4. Quick Check:

    Exception in try -> catch runs [OK]
Hint: Errors in try always jump to catch block [OK]
Common Mistakes:
  • Thinking the program ignores errors in try
  • Assuming the program restarts try block
  • Believing the program stops without catch
2. Which of the following is the correct syntax to catch an exception in C#?
easy
A. try { /* code */ } catch (ex Exception) { /* handle */ }
B. try { /* code */ } catch Exception ex { /* handle */ }
C. try { /* code */ } catch { Exception ex } { /* handle */ }
D. try { /* code */ } catch (Exception ex) { /* handle */ }

Solution

  1. Step 1: Recall correct catch syntax

    The catch block must have parentheses with exception type and variable: catch (Exception ex).
  2. Step 2: Identify correct option

    Only try { /* code */ } catch (Exception ex) { /* handle */ } uses the correct syntax with parentheses and exception variable.
  3. Final Answer:

    try { /* code */ } catch (Exception ex) { /* handle */ } -> Option D
  4. Quick Check:

    Correct catch syntax = catch (Exception ex) [OK]
Hint: Catch needs parentheses with exception type and variable [OK]
Common Mistakes:
  • Omitting parentheses around exception
  • Swapping exception type and variable order
  • Using braces instead of parentheses
3. What will be the output of this C# code?
try {
    Console.WriteLine("Start");
    int x = 5 / 0;
    Console.WriteLine("End");
} catch (DivideByZeroException) {
    Console.WriteLine("Error caught");
}
medium
A. Start\nError caught
B. Error caught
C. Start\nEnd
D. Start

Solution

  1. Step 1: Trace code inside try block

    "Start" prints first. Then division by zero causes an exception.
  2. Step 2: Exception triggers catch block

    Catch block runs and prints "Error caught". The line after division is skipped.
  3. Final Answer:

    Start\nError caught -> Option A
  4. Quick Check:

    Exception skips rest of try, catch prints message [OK]
Hint: Exception skips rest of try, catch runs next [OK]
Common Mistakes:
  • Assuming 'End' prints after exception
  • Thinking catch runs before 'Start'
  • Ignoring exception and continuing try
4. Identify the error in this C# code snippet:
try {
    int[] arr = new int[2];
    arr[3] = 10;
} catch (Exception e) {
    Console.WriteLine("Exception caught");
}
medium
A. Array index out of bounds exception is not caught.
B. The catch block syntax is incorrect.
C. The code will throw an exception but catch block handles it correctly.
D. The try block has a syntax error.

Solution

  1. Step 1: Analyze the try block code

    Accessing index 3 in an array of size 2 causes an IndexOutOfRangeException.
  2. Step 2: Check catch block handling

    Catch block catches all exceptions of type Exception, so it will handle this error and print the message.
  3. Final Answer:

    The code will throw an exception but catch block handles it correctly. -> Option C
  4. Quick Check:

    Exception thrown and caught properly [OK]
Hint: Catch(Exception e) catches all exceptions [OK]
Common Mistakes:
  • Thinking catch syntax is wrong
  • Assuming exception is not caught
  • Believing try block has syntax error
5. Consider this code:
try {
    Console.WriteLine("A");
    try {
        int y = int.Parse("abc");
    } catch (FormatException) {
        Console.WriteLine("Format error");
    }
    Console.WriteLine("B");
} catch (Exception) {
    Console.WriteLine("General error");
}

What will be the output?
hard
A. A\nGeneral error\nB
B. A\nFormat error\nB
C. Format error\nB
D. A\nB

Solution

  1. Step 1: Trace outer try block

    Prints "A" first, then enters inner try block.
  2. Step 2: Inner try-catch handles FormatException

    Parsing "abc" causes FormatException, caught by inner catch which prints "Format error".
  3. Step 3: Continue outer try after inner catch

    After inner catch, prints "B". Outer catch is not triggered.
  4. Final Answer:

    A\nFormat error\nB -> Option B
  5. Quick Check:

    Inner catch handles error, outer continues [OK]
Hint: Inner catch handles error, outer try continues after [OK]
Common Mistakes:
  • Assuming outer catch runs instead of inner
  • Thinking code stops after inner exception
  • Missing that 'B' prints after inner catch