![]() |
VOOZH | about |
In C#, an exception is an unexpected event that occurs during the execution of a program, which disrupts the normal flow of the program. Exceptions can occur due to various reasons such as invalid input, division by zero, file not found or network issues.
Example:
Exceptions in C# can be broadly classified into two types:
1. System Exceptions
2. Application Exceptions
Occurs when a number is divided by zero.
Occurs when trying to access a member of an object that is null.
Occurs when accessing an array element outside its valid range (size of array).
You can define your own exceptions by extending ApplicationException.
The Exception class in C# provides several properties to retrieve detailed information about an exception when it occurs:
| Feature | Exception | Error |
|---|---|---|
| Definition | Unexpected condition in program flow | Serious problem that cannot be handled in normal program flow |
| Recoverability | Can be handled or recovered | Usually cannot be handled |
| Occurrence | Happens at runtime | Happens at compile-time or runtime (system-level) |
| Examples | NullReferenceException, DivideByZeroException | StackOverflowError, OutOfMemoryError |
| Purpose | Indicates conditions that can be managed | Indicates serious problems beyond program control |