Exceptions help your program handle errors without crashing. The exception hierarchy organizes different error types so you can catch and fix them properly.
Exception hierarchy in .NET in C Sharp (C#)
Start learning this pattern below
Jump into concepts and practice - no test required
or
Test this pattern10 questions across easy, medium, and hard to know if this pattern is strong
Introduction
Syntax
C Sharp (C#)
try { // code that might cause an error } catch (ExceptionType ex) { // code to handle that error } finally { // code that runs no matter what }
try block contains code that might cause an error.
catch block handles specific error types.
Examples
C Sharp (C#)
try { int x = 5 / 0; } catch (DivideByZeroException ex) { Console.WriteLine("Cannot divide by zero."); }
C Sharp (C#)
try { string s = null; Console.WriteLine(s.Length); } catch (NullReferenceException ex) { Console.WriteLine("Object was null."); }
C Sharp (C#)
try { // some code } catch (Exception ex) { Console.WriteLine("Some error happened."); }
Sample Program
This program tries to access an array element that does not exist. It catches the specific error and also has a general catch. The finally block runs always.
C Sharp (C#)
using System; class Program { static void Main() { try { int[] numbers = {1, 2, 3}; Console.WriteLine(numbers[5]); } catch (IndexOutOfRangeException ex) { Console.WriteLine("Index was outside the bounds of the array."); } catch (Exception ex) { Console.WriteLine("Some other error occurred."); } finally { Console.WriteLine("This runs no matter what."); } } }
Important Notes
All exceptions in .NET inherit from System.Exception.
You can catch specific exceptions first, then more general ones.
The finally block is useful for cleanup code that must run.
Summary
Exceptions are organized in a hierarchy starting from System.Exception.
Catching specific exceptions helps handle errors better.
Use try-catch-finally blocks to manage errors safely.
Practice
1. Which class is the base class for all exceptions in .NET?
easy
Solution
Step 1: Understand the exception hierarchy
All exceptions in .NET inherit from a common base class to unify error handling.Step 2: Identify the root base class
The root base class for all exceptions isSystem.Exception, from which other exceptions derive.Final Answer:
System.Exception -> Option BQuick 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
Solution
Step 1: Recall catch block syntax
In C#, to catch a specific exception, usecatch (ExceptionType variable)syntax.Step 2: Match the correct syntax for ArgumentNullException
The correct syntax iscatch (ArgumentNullException e) { }, which declares the exception type and variable.Final Answer:
catch (ArgumentNullException e) { } -> Option AQuick 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
Solution
Step 1: Identify the exception thrown
Accessing index 5 in an array of size 2 throwsIndexOutOfRangeException.Step 2: Check which catch block handles it
The first catch block specifically catchesIndexOutOfRangeException, so it runs and prints "Index error caught".Final Answer:
Index error caught -> Option DQuick 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
Solution
Step 1: Review catch block syntax
Catch blocks can omit the exception variable if not used, which is valid here.Step 2: Check catch block order
The specificFormatExceptioncatch is before the general catch-all block, which is correct.Final Answer:
No error, code is correct -> Option CQuick 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
Solution
Step 1: Understand the requirement
We want to handle all exceptions exceptNullReferenceExceptiondifferently, so we must detect and exclude it.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 isNullReferenceException, effectively excluding it from handling.Final Answer:
Use a catch block for Exception and rethrow if NullReferenceException -> Option AQuick 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
