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

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

Choose your learning style9 modes available
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.