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

Multiple catch blocks in C Sharp (C#) - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Exception Mastery Badge
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2: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");
        }
    }
}
AIndex out of range caught
BNo output, program crashes
CNull reference caught
DGeneral exception caught
Attempts:
2 left
💡 Hint
Think about which catch block matches the thrown exception type exactly.
Predict Output
intermediate
2: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");
        }
    }
}
ANo output, program crashes
BGeneral exception caught
CArgument exception caught
DNull reference caught
Attempts:
2 left
💡 Hint
Which catch block matches the exception type exactly?
Predict Output
advanced
2: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");
        }
    }
}
AFormat exception caught
BNo output, program crashes
CCompilation error due to catch order
DGeneral exception caught
Attempts:
2 left
💡 Hint
Check the order of catch blocks and C# rules about exception handling.
Predict Output
advanced
2: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");
        }
    }
}
AInvalid operation caught
BAggregate exception caught
CGeneral exception caught
DNo output, program crashes
Attempts:
2 left
💡 Hint
Task.WaitAll wraps exceptions in AggregateException.
🧠 Conceptual
expert
2:00remaining
Order of multiple catch blocks and exception handling
Which statement about multiple catch blocks in C# is correct?
ACatch blocks must be ordered from most specific to most general exception types to compile.
BCatch blocks must be ordered from most general to most specific exception types.
CCatch blocks can be in any order; the first matching block executes at runtime.
DOnly one catch block is allowed per try block in C#.
Attempts:
2 left
💡 Hint
Think about how C# compiler checks catch block order.