📖 Scenario: Imagine you are building a simple program that processes user input and might encounter different types of errors. You want to understand how exceptions are organized in .NET so you can handle them properly.
🎯 Goal: You will create a small program that defines a few exceptions following the .NET exception hierarchy and demonstrates catching them in order.
📋 What You'll Learn
Create a base exception class called MyAppException that inherits from System.Exception.
Create two derived exception classes: InputException and ProcessingException that inherit from MyAppException.
Write a method ProcessInput that throws InputException if input is empty and ProcessingException if input is "error".
Use a try-catch block to catch exceptions in order: first InputException, then ProcessingException, then MyAppException, and finally Exception.
💡 Why This Matters
🌍 Real World
Understanding exception hierarchy helps you write robust programs that can handle errors clearly and maintainably.
💼 Career
Many software development jobs require good error handling skills to build reliable applications.
Progress0 / 4 steps
1
Create base exception class
Create a class called MyAppException that inherits from System.Exception and has a constructor that takes a string message and passes it to the base constructor.
C Sharp (C#)
Hint
Remember to inherit from Exception and call the base constructor with the message.
2
Create derived exception classes
Create two classes called InputException and ProcessingException that both inherit from MyAppException. Each should have a constructor that takes a string message and passes it to the base constructor.
C Sharp (C#)
Hint
Both classes inherit from MyAppException and call the base constructor with the message.
3
Write method that throws exceptions
Write a static method called ProcessInput that takes a string input. If input is empty, throw an InputException with message "Input is empty". If input is "error", throw a ProcessingException with message "Processing error occurred". Otherwise, do nothing.
C Sharp (C#)
Hint
Use string.IsNullOrEmpty(input) to check for empty input and throw the correct exceptions.
4
Catch exceptions in order
In the Main method, call ProcessInput three times with inputs: empty string, "error", and "ok". Use a try-catch block to catch exceptions in this order: InputException, ProcessingException, MyAppException, and Exception. Print the caught exception's message with Console.WriteLine. If no exception occurs, print "Input processed successfully".
C Sharp (C#)
Hint
Remember to catch exceptions from most specific to most general. Print the exception message or success message accordingly.
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
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 is System.Exception, from which other exceptions derive.
Final Answer:
System.Exception -> Option B
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
Step 1: Recall catch block syntax
In C#, to catch a specific exception, use catch (ExceptionType variable) syntax.
Step 2: Match the correct syntax for ArgumentNullException
The correct syntax is catch (ArgumentNullException e) { }, which declares the exception type and variable.
Accessing index 5 in an array of size 2 throws IndexOutOfRangeException.
Step 2: Check which catch block handles it
The first catch block specifically catches IndexOutOfRangeException, so it runs and prints "Index error caught".
Final Answer:
Index error caught -> Option D
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
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 specific FormatException catch is before the general catch-all block, which is correct.
Final Answer:
No error, code is correct -> Option C
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
Step 1: Understand the requirement
We want to handle all exceptions except NullReferenceException differently, 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 is NullReferenceException, effectively excluding it from handling.
Final Answer:
Use a catch block for Exception and rethrow if NullReferenceException -> Option A
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
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.