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

Multiple catch blocks 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
🎖️
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.

Practice

(1/5)
1.

What is the main purpose of using multiple catch blocks in C#?

easy
A. To avoid using try blocks
B. To handle different types of exceptions separately
C. To make the code run faster
D. To declare multiple variables

Solution

  1. Step 1: Understand the role of catch blocks

    catch blocks are used to handle errors that happen in the try block.
  2. Step 2: Recognize why multiple catch blocks are used

    Using multiple catch blocks lets you respond differently to different error types, making your program clearer and safer.
  3. Final Answer:

    To handle different types of exceptions separately -> Option B
  4. Quick Check:

    Multiple catch blocks = handle different exceptions [OK]
Hint: Multiple catch blocks handle different errors separately [OK]
Common Mistakes:
  • Thinking multiple catch blocks speed up code
  • Believing catch blocks replace try blocks
  • Using catch blocks to declare variables
2.

Which of the following is the correct syntax order for multiple catch blocks in C#?

try { ... }
catch (ArgumentNullException e) { ... }
catch (Exception e) { ... }
easy
A. Specific exceptions first, general exceptions last
B. General exceptions first, specific exceptions last
C. Order does not matter
D. Only one catch block is allowed

Solution

  1. Step 1: Understand exception hierarchy

    Specific exceptions like ArgumentNullException inherit from general exceptions like Exception.
  2. Step 2: Order catch blocks correctly

    Place specific exceptions first so they catch their errors before the general catch block handles all others.
  3. Final Answer:

    Specific exceptions first, general exceptions last -> Option A
  4. Quick Check:

    Specific before general catch blocks [OK]
Hint: Put specific exceptions before general ones in catch blocks [OK]
Common Mistakes:
  • Placing general catch before specific causes unreachable code
  • Assuming catch order does not matter
  • Trying to use multiple catch blocks without try
3.

What will be the output of this C# code?

try {
    int[] arr = new int[2];
    Console.WriteLine(arr[5]);
} catch (IndexOutOfRangeException) {
    Console.WriteLine("Index error caught");
} catch (Exception) {
    Console.WriteLine("General error caught");
}
medium
A. No output, program crashes
B. General error caught
C. Index error caught
D. Compilation error

Solution

  1. Step 1: Identify the exception thrown

    Accessing arr[5] causes an IndexOutOfRangeException because the array size is 2.
  2. Step 2: Match exception to catch block

    The first catch block matches IndexOutOfRangeException, so it runs and prints "Index error caught".
  3. Final Answer:

    Index error caught -> Option C
  4. Quick Check:

    IndexOutOfRangeException triggers first catch [OK]
Hint: Exception type matches first suitable catch block [OK]
Common Mistakes:
  • Thinking general catch runs before specific
  • Assuming no exception occurs
  • Confusing exception types
4.

Find the error in this code snippet:

try {
    int x = int.Parse("abc");
} catch (Exception e) {
    Console.WriteLine("General error");
} catch (FormatException e) {
    Console.WriteLine("Format error");
}
medium
A. The FormatException catch block is unreachable
B. Missing finally block
C. Try block syntax is incorrect
D. No error, code is correct

Solution

  1. Step 1: Check catch block order

    The first catch block catches all Exception types, including FormatException.
  2. Step 2: Identify unreachable catch block

    Since Exception catch is first, the FormatException catch block can never run, causing a compile error.
  3. Final Answer:

    The FormatException catch block is unreachable -> Option A
  4. Quick Check:

    General catch before specific causes unreachable code [OK]
Hint: Place specific catch blocks before general ones to avoid unreachable code [OK]
Common Mistakes:
  • Putting general catch before specific
  • Ignoring unreachable code errors
  • Thinking finally block is mandatory
5.

You want to handle NullReferenceException and DivideByZeroException differently, but also catch any other exceptions generally. Which is the best order of catch blocks?

try {
    // code that may throw exceptions
} catch (___) {
    Console.WriteLine("Null reference error");
} catch (___) {
    Console.WriteLine("Divide by zero error");
} catch (___) {
    Console.WriteLine("Other error");
}
hard
A. Exception, DivideByZeroException, NullReferenceException
B. Exception, NullReferenceException, DivideByZeroException
C. DivideByZeroException, Exception, NullReferenceException
D. NullReferenceException, DivideByZeroException, Exception

Solution

  1. Step 1: Identify specific exceptions

    NullReferenceException and DivideByZeroException are specific exceptions to catch first.
  2. Step 2: Place general exception last

    The general Exception catch block should come last to catch all other exceptions.
  3. Final Answer:

    NullReferenceException, DivideByZeroException, Exception -> Option D
  4. Quick Check:

    Specific exceptions first, general last [OK]
Hint: Catch specific exceptions before general Exception last [OK]
Common Mistakes:
  • Putting Exception catch first
  • Mixing order of specific exceptions
  • Omitting general catch block