Challenge - 5 Problems
Async Exception Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of async method with try-catch
What is the output of this C# program when run?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static async Task Main() { try { await ThrowExceptionAsync(); } catch (Exception ex) { Console.WriteLine($"Caught: {ex.Message}"); } } static async Task ThrowExceptionAsync() { await Task.Delay(10); throw new InvalidOperationException("Error happened"); } }
Attempts:
2 left
💡 Hint
Remember that exceptions thrown inside awaited async methods can be caught by try-catch around the await.
✗ Incorrect
The async method ThrowExceptionAsync throws an exception after a delay. The Main method awaits it inside a try-catch block, so the exception is caught and its message printed.
❓ Predict Output
intermediate2:00remaining
Exception behavior without await
What happens when this C# program runs?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static async Task Main() { try { ThrowExceptionAsync(); } catch (Exception ex) { Console.WriteLine($"Caught: {ex.Message}"); } await Task.Delay(50); Console.WriteLine("End of Main"); } static async Task ThrowExceptionAsync() { await Task.Delay(10); throw new InvalidOperationException("Error happened"); } }
Attempts:
2 left
💡 Hint
Think about what happens if you don't await an async method that throws an exception.
✗ Incorrect
The exception in ThrowExceptionAsync is not caught because the method is not awaited inside the try-catch. The exception is thrown later and crashes the program after printing "End of Main".
🔧 Debug
advanced2:00remaining
Identify the cause of unhandled exception
Why does this program crash with an unhandled exception despite the try-catch block?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static async Task Main() { try { var task = ThrowExceptionAsync(); } catch (Exception ex) { Console.WriteLine($"Caught: {ex.Message}"); } await Task.Delay(50); } static async Task ThrowExceptionAsync() { await Task.Delay(10); throw new InvalidOperationException("Error happened"); } }
Attempts:
2 left
💡 Hint
Consider when exceptions in async methods are observed and caught.
✗ Incorrect
The exception happens inside the async method after the try-catch block has finished because the task is not awaited inside the try. The exception is unobserved until later, causing a crash.
📝 Syntax
advanced2:00remaining
Which code snippet correctly catches exceptions from multiple awaited tasks?
Select the code snippet that correctly catches exceptions from two async methods awaited in parallel.
Attempts:
2 left
💡 Hint
Think about how to await multiple tasks and catch exceptions from all of them.
✗ Incorrect
Option B uses Task.WhenAll to await both tasks in parallel and catches exceptions from either. Option B awaits sequentially, which works but is not parallel. Option B awaits tasks separately inside try, but exceptions from the first await may prevent the second. Option B uses Task.WaitAll which blocks and can cause deadlocks in async context.
🚀 Application
expert3:00remaining
Determine the final value after exception handling in async code
What is the value of variable 'result' after this code runs?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static int result = 0; static async Task Main() { try { await MethodA(); } catch { result += 10; } result += 1; Console.WriteLine(result); } static async Task MethodA() { try { await MethodB(); } catch { result += 5; throw; } } static async Task MethodB() { await Task.Delay(10); throw new Exception("Fail"); } }
Attempts:
2 left
💡 Hint
Trace the increments to 'result' through nested try-catch and rethrow.
✗ Incorrect
MethodB throws an exception. MethodA catches it, adds 5 to result, then rethrows. Main catches it, adds 10 to result, then adds 1. Total: 0 + 5 + 10 + 1 = 16.