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

Exception hierarchy in .NET in C Sharp (C#) - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Exception hierarchy in .NET
Start: Exception occurs
Is it a System.Exception?
Is it SystemException?
Specific Exception Type
Catch or Propagate
End
When an error happens, .NET checks if it is an Exception, then narrows down to SystemException or ApplicationException, then to specific types, deciding how to handle it.
Execution Sample
C Sharp (C#)
try {
  throw new ArgumentNullException("param");
} catch (Exception e) {
  Console.WriteLine(e.GetType().Name);
}
Throws an ArgumentNullException and catches it as Exception, printing its type name.
Execution Table
StepActionException TypeResultOutput
1Throw new ArgumentNullExceptionArgumentNullExceptionException thrown
2Catch block checks ExceptionArgumentNullExceptionCaught by Exception catch
3Print exception type nameArgumentNullExceptionPrintedArgumentNullException
4End of try-catch--Program continues
💡 Exception caught and handled, program continues normally
Variable Tracker
VariableStartAfter ThrowAfter CatchFinal
enullArgumentNullException instanceArgumentNullException instanceArgumentNullException instance
Key Moments - 2 Insights
Why does the catch block catch ArgumentNullException when it says catch(Exception e)?
Because ArgumentNullException inherits from Exception, so catch(Exception e) catches all exceptions derived from Exception as shown in execution_table step 2.
What is the difference between SystemException and ApplicationException?
SystemException is for system errors (like ArgumentNullException), ApplicationException is for user-defined exceptions. The hierarchy narrows exceptions into these categories as in concept_flow.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the output printed at step 3?
AException
BArgumentNullException
CNullReferenceException
DNo output
💡 Hint
Check the Output column at step 3 in execution_table
At which step does the exception get caught?
AStep 2
BStep 1
CStep 3
DStep 4
💡 Hint
Look at the Result column in execution_table where it says 'Caught by Exception catch'
If we throw a custom exception inheriting from ApplicationException, which catch block would catch it?
AOnly catch(ApplicationException e)
BOnly catch(SystemException e)
Ccatch(Exception e)
DNo catch block
💡 Hint
Refer to concept_flow and key_moments about inheritance and catch blocks
Concept Snapshot
Exception hierarchy in .NET:
- Base: System.Exception
- Two main branches: SystemException (system errors), ApplicationException (user-defined)
- Specific exceptions inherit from these
- catch(Exception e) catches all exceptions
- Use specific catches for finer control
Full Transcript
In .NET, exceptions form a hierarchy starting from System.Exception. When an error occurs, the runtime checks if it is an Exception, then narrows down to SystemException or ApplicationException, and then to specific exception types like ArgumentNullException. In the example, throwing ArgumentNullException is caught by a catch block for Exception because ArgumentNullException inherits from Exception. This shows how catch blocks can catch exceptions of derived types. SystemException is for system errors, ApplicationException for user-defined errors. Understanding this hierarchy helps write better error handling code.