Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Difference between system level exception and Application level exception.
An exception is an unwanted event that interrupts the normal flow of a program. In C#, exceptions are broadly categorized into System Level Exceptions (thrown by the CLR for fatal errors) and Application Level Exceptions (thrown by application code for recoverable errors).
System Level Exception
System level exceptions are derived from System.SystemException and are thrown by the .NET Common Language Runtime (CLR). They represent non-recoverable or fatal errors such as stack overflow, out of memory, null reference, or database crashes. These exceptions are generally not handled by application code.
Common examples − NullReferenceException, StackOverflowException, OutOfMemoryException, IndexOutOfRangeException.
Application Level Exception
Application level exceptions are derived from System.ApplicationException and are thrown by application code when recoverable errors occur, such as invalid input data or business rule violations. These are handled using try-catch blocks and developers can create custom exceptions by extending ApplicationException.
Common examples − ArgumentException, FormatException, custom business logic exceptions.
Example
The following example shows a custom application level exception in C# ?
using System;
// Custom Application Level Exception
class InvalidAgeException : ApplicationException {
public InvalidAgeException(string message) : base(message) { }
}
class Program {
static void ValidateAge(int age) {
if (age < 0 || age > 150)
throw new InvalidAgeException("Age must be between 0 and 150");
}
static void Main() {
try {
ValidateAge(200); // Application exception (recoverable)
}
catch (InvalidAgeException ex) {
Console.WriteLine("Application Exception: " + ex.Message);
}
try {
int[] arr = {1, 2, 3};
Console.WriteLine(arr[10]); // System exception
}
catch (IndexOutOfRangeException ex) {
Console.WriteLine("System Exception: " + ex.Message);
}
}
}
The output of the above code is ?
Application Exception: Age must be between 0 and 150 System Exception: Index was outside the bounds of the array.
Key Differences
| Feature | System Level Exception | Application Level Exception |
|---|---|---|
| Base Class | System.SystemException |
System.ApplicationException |
| Thrown By | CLR (.NET runtime) | Application code |
| Error Type | Fatal, non-recoverable | Recoverable, business logic errors |
| Handling | Difficult or impossible to handle gracefully | Handled using try-catch blocks |
| Custom Exceptions | Not supported | Supported (extend ApplicationException) |
| Examples | NullReferenceException, StackOverflowException | ArgumentException, custom exceptions |
Conclusion
System level exceptions are thrown by the CLR for critical, often non-recoverable errors. Application level exceptions are thrown by developers for recoverable business logic errors and can be customized. Both derive from System.Exception, but serve different purposes in error handling strategy.
