Challenge - 5 Problems
Exception Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of multiple catch blocks with specific exceptions
What is the output of this C# code when an IndexOutOfRangeException is thrown?
C Sharp (C#)
using System; class Program { static void Main() { try { int[] arr = new int[2]; Console.WriteLine(arr[5]); } catch (NullReferenceException) { Console.WriteLine("Null reference caught"); } catch (IndexOutOfRangeException) { Console.WriteLine("Index out of range caught"); } catch (Exception) { Console.WriteLine("General exception caught"); } } }
Attempts:
2 left
💡 Hint
Think about which catch block matches the thrown exception type exactly.
✗ Incorrect
The thrown exception is IndexOutOfRangeException, so the catch block for that specific exception runs, printing "Index out of range caught".
❓ Predict Output
intermediate2:00remaining
Which catch block executes for a NullReferenceException?
Given this code, what will be printed when a NullReferenceException occurs?
C Sharp (C#)
using System; class Program { static void Main() { try { string s = null; Console.WriteLine(s.Length); } catch (ArgumentException) { Console.WriteLine("Argument exception caught"); } catch (NullReferenceException) { Console.WriteLine("Null reference caught"); } catch (Exception) { Console.WriteLine("General exception caught"); } } }
Attempts:
2 left
💡 Hint
Which catch block matches the exception type exactly?
✗ Incorrect
NullReferenceException is caught by the matching catch block, so it prints "Null reference caught".
❓ Predict Output
advanced2:00remaining
Output when multiple catch blocks could match
What will this code print when a FormatException is thrown?
C Sharp (C#)
using System; class Program { static void Main() { try { int.Parse("abc"); } catch (FormatException) { Console.WriteLine("Format exception caught"); } catch (Exception) { Console.WriteLine("General exception caught"); } } }
Attempts:
2 left
💡 Hint
Check the order of catch blocks and C# rules about exception handling.
✗ Incorrect
In C#, catch blocks must be ordered from most specific to most general. Catching Exception before FormatException causes a compile-time error.
❓ Predict Output
advanced2:00remaining
Which catch block handles AggregateException?
What will be printed when this code throws an AggregateException?
C Sharp (C#)
using System; using System.Threading.Tasks; class Program { static void Main() { try { Task.WaitAll(Task.Run(() => throw new InvalidOperationException())); } catch (InvalidOperationException) { Console.WriteLine("Invalid operation caught"); } catch (AggregateException) { Console.WriteLine("Aggregate exception caught"); } catch (Exception) { Console.WriteLine("General exception caught"); } } }
Attempts:
2 left
💡 Hint
Task.WaitAll wraps exceptions in AggregateException.
✗ Incorrect
Task.WaitAll throws AggregateException which is caught by the matching catch block printing "Aggregate exception caught".
🧠 Conceptual
expert2:00remaining
Order of multiple catch blocks and exception handling
Which statement about multiple catch blocks in C# is correct?
Attempts:
2 left
💡 Hint
Think about how C# compiler checks catch block order.
✗ Incorrect
C# requires catch blocks to be ordered from most specific to most general exception types to avoid unreachable code errors.