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

Exception hierarchy in .NET 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 in .NET
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
What is the output of this code snippet?

Consider the following C# code that catches exceptions:

try {
    throw new System.IO.FileNotFoundException("File missing");
} catch (System.Exception ex) {
    Console.WriteLine(ex.GetType().Name);
}

What will be printed?

C Sharp (C#)
try {
    throw new System.IO.FileNotFoundException("File missing");
} catch (System.Exception ex) {
    Console.WriteLine(ex.GetType().Name);
}
AFileNotFoundException
BSystemException
CException
DIOException
Attempts:
2 left
💡 Hint

FileNotFoundException inherits from IOException, which inherits from Exception.

Predict Output
intermediate
2:00remaining
What exception type is caught here?

Given this code snippet:

try {
    throw new System.ArgumentNullException("param");
} catch (System.SystemException ex) {
    Console.WriteLine(ex.GetType().Name);
}

What will be printed?

C Sharp (C#)
try {
    throw new System.ArgumentNullException("param");
} catch (System.SystemException ex) {
    Console.WriteLine(ex.GetType().Name);
}
ASystemException
BInvalidOperationException
CException
DArgumentNullException
Attempts:
2 left
💡 Hint

ArgumentNullException inherits from SystemException.

Predict Output
advanced
2:00remaining
What error does this code raise?

Examine this code snippet:

try {
    throw new System.Exception("Error");
} catch (System.IO.IOException ex) {
    Console.WriteLine("IO error");
} catch (System.Exception ex) {
    Console.WriteLine("General error");
}

What will be printed?

C Sharp (C#)
try {
    throw new System.Exception("Error");
} catch (System.IO.IOException ex) {
    Console.WriteLine("IO error");
} catch (System.Exception ex) {
    Console.WriteLine("General error");
}
AGeneral error
BIO error
CNo output, program crashes
DCompilation error
Attempts:
2 left
💡 Hint

Exception is not an IOException, but it is an Exception.

Predict Output
advanced
2:00remaining
What is the output of this code with nested exceptions?

Look at this code:

try {
    try {
        throw new System.IndexOutOfRangeException("Index error");
    } catch (System.Exception ex) {
        throw new System.ApplicationException("App error", ex);
    }
} catch (System.ApplicationException ex) {
    Console.WriteLine(ex.InnerException.GetType().Name);
}

What will be printed?

C Sharp (C#)
try {
    try {
        throw new System.IndexOutOfRangeException("Index error");
    } catch (System.Exception ex) {
        throw new System.ApplicationException("App error", ex);
    }
} catch (System.ApplicationException ex) {
    Console.WriteLine(ex.InnerException.GetType().Name);
}
AException
BApplicationException
CIndexOutOfRangeException
DNullReferenceException
Attempts:
2 left
💡 Hint

The inner exception is the original exception thrown inside the inner try block.

🧠 Conceptual
expert
2:00remaining
Which exception type is the base for all CLR exceptions?

In the .NET exception hierarchy, which class is the ultimate base class for all exceptions thrown by the Common Language Runtime (CLR)?

ASystem.ApplicationException
BSystem.Exception
CSystem.SystemException
DSystem.Object
Attempts:
2 left
💡 Hint

Think about the root class that all exceptions inherit from.

Practice

(1/5)
1. Which class is the base class for all exceptions in .NET?
easy
A. System.Error
B. System.Exception
C. System.ApplicationException
D. System.SystemException

Solution

  1. Step 1: Understand the exception hierarchy

    All exceptions in .NET inherit from a common base class to unify error handling.
  2. Step 2: Identify the root base class

    The root base class for all exceptions is System.Exception, from which other exceptions derive.
  3. Final Answer:

    System.Exception -> Option B
  4. Quick Check:

    Base exception class = System.Exception [OK]
Hint: Remember: All exceptions come from System.Exception [OK]
Common Mistakes:
  • Confusing System.SystemException as the base
  • Thinking System.ApplicationException is the root
  • Assuming System.Error exists in .NET
2. Which of the following is the correct syntax to catch a specific exception type ArgumentNullException in C#?
easy
A. catch (ArgumentNullException e) { }
B. catch ArgumentNullException (e) { }
C. catch (Exception e) { }
D. catch ArgumentNullException { }

Solution

  1. Step 1: Recall catch block syntax

    In C#, to catch a specific exception, use catch (ExceptionType variable) syntax.
  2. Step 2: Match the correct syntax for ArgumentNullException

    The correct syntax is catch (ArgumentNullException e) { }, which declares the exception type and variable.
  3. Final Answer:

    catch (ArgumentNullException e) { } -> Option A
  4. Quick Check:

    Correct catch syntax = catch (ExceptionType e) [OK]
Hint: Use parentheses around exception type and variable in catch [OK]
Common Mistakes:
  • Omitting parentheses around exception type
  • Using wrong order like catch ExceptionType (e)
  • Not declaring a variable for the exception
3. What will be the output of the following C# code?
try {
    int[] arr = new int[2];
    Console.WriteLine(arr[5]);
} catch (IndexOutOfRangeException e) {
    Console.WriteLine("Index error caught");
} catch (Exception e) {
    Console.WriteLine("General error caught");
}
medium
A. General error caught
B. Compilation error
C. No output, program crashes
D. Index error caught

Solution

  1. Step 1: Identify the exception thrown

    Accessing index 5 in an array of size 2 throws IndexOutOfRangeException.
  2. Step 2: Check which catch block handles it

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

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

    Specific catch runs before general [OK]
Hint: Specific exceptions catch before general ones [OK]
Common Mistakes:
  • Thinking general Exception catch runs first
  • Assuming program crashes without catch
  • Confusing IndexOutOfRangeException with ArgumentException
4. Identify the error in this code snippet:
try {
    int x = int.Parse("abc");
} catch (FormatException) {
    Console.WriteLine("Format error");
} catch {
    Console.WriteLine("General error");
}
medium
A. Catch-all block must be last
B. Catch blocks order is incorrect
C. No error, code is correct
D. Missing exception variable in catch blocks

Solution

  1. Step 1: Review catch block syntax

    Catch blocks can omit the exception variable if not used, which is valid here.
  2. Step 2: Check catch block order

    The specific FormatException catch is before the general catch-all block, which is correct.
  3. Final Answer:

    No error, code is correct -> Option C
  4. Quick Check:

    Catch-all last and variable optional [OK]
Hint: Catch-all must be last; variable optional in catch [OK]
Common Mistakes:
  • Thinking catch-all must have variable
  • Placing catch-all before specific catch
  • Assuming variable is mandatory in catch
5. You want to catch all exceptions except NullReferenceException and handle them differently. Which approach correctly implements this in C#?
hard
A. Use a catch block for Exception and rethrow if NullReferenceException
B. Use a single catch block for Exception and check exception type inside
C. Use two catch blocks: one for NullReferenceException, one for Exception
D. Use a catch block for NullReferenceException only

Solution

  1. Step 1: Understand the requirement

    We want to handle all exceptions except NullReferenceException differently, so we must detect and exclude it.
  2. Step 2: Evaluate approaches

    Use a catch block for Exception and rethrow if NullReferenceException uses a general catch for Exception, then rethrows if the exception is NullReferenceException, effectively excluding it from handling.
  3. Final Answer:

    Use a catch block for Exception and rethrow if NullReferenceException -> Option A
  4. Quick Check:

    Rethrow to exclude specific exceptions [OK]
Hint: Rethrow specific exceptions inside general catch [OK]
Common Mistakes:
  • Catching NullReferenceException first but not rethrowing
  • Handling all exceptions in one catch without rethrow
  • Using only NullReferenceException catch block