Open In App

C# System Level Exception vs Application Level Exception

Last Updated : 25 Jan, 2025
Comments
Improve
Suggest changes
Like Article
Like
Report

In C#, exceptions are categorized into two main types based on their origin and usage, which are System-level exceptions and Application-level exceptions. Understanding the difference between these two helps in managing exceptions properly and choosing the right exception-handling approach.

System Level Exception vs Application Level Exception

Feature

System Level Exception

Application Level Exception

Origin

This is generated by the CLR or system environment.

This is defined and thrown by the application code.

Examples

NullReferenceException, OutOfMemoryException, etc.

FileNotFoundException, InvalidUserInputException, etc.

Recoverability

This is difficult or impossible to recover from.

Sometimes it is recoverable and handled by the application logic.

Handling

This is not caught unless performing cleanup before termination.

Caught in try-catch blocks, and appropriate corrective action is taken.

Purpose

It indicates serious issues with the runtime environment.

It handles business logic errors or invalid input within the application.

System-Level vs Application-Level Exception Hierarchy in C#

CSharp-Exception

System-Level Exceptions

System-level exceptions are errors that occur due to issues with the system environment, such as problems with the runtime environment or hardware-related failures. These exceptions are generated by the .NET runtime (CLR) and are often not recoverable by the application.

Key Characteristics:

  • Origin: These are generated by the Common Language Runtime (CLR) during program execution.
  • Examples:
    • DivideByZeroException: This occurs when there is an attempt to divide a number by zero.
    • NullReferenceException: This occurs when an object reference is null but is being accessed.
    • OutOfMemoryException: This occurs when the system runs out of memory.
    • StackOverflowException: This is caused by excessive recursion that fills the call stack.
    • IndexOutOfRangeException: This occurs when trying to access an invalid index in an array or collection.
  • Recovery: These exceptions usually represent system-related failures that are difficult or impossible to recover from. They sometimes lead to program termination.
  • Handling: It is generally not recommended to catch these exceptions unless you are trying to log or perform cleanup before exiting the program.

Application-Level Exceptions

Application-level exceptions, also known as custom exceptions, are errors that occurs from logical issues in the application code. These exceptions are defined by the application developer to handle specific business logic errors or invalid inputs.

Key Characteristics:

  • Origin: These are created by the developer to handle specific application logic failures or expected error conditions.
  • Examples:
    • FileNotFoundException: This can occur if the application tries to access a file that doesn’t exist.
    • InvalidUserInputException: A custom exception thrown when the user enters invalid data.
    • PaymentProcessingException: A custom exception thrown in an e-commerce application when there is an issue with payment processing.
  • Recovery: These exceptions are often recoverable and can be handled by the application logic. For example, we may ask the user to correct input or retry an operation.
  • Handling: Application-level exceptions are sometimes caught and managed by using try-catch blocks, and proper recovery or corrective actions can be taken.


Next Article

Similar Reads