![]() |
VOOZH | about |
In C#, exceptions are used to handle unexpected situations in a program. The .NET Framework provides many built-in exception classes such as DivideByZeroException, NullReferenceException or IndexOutOfRangeException. However, sometimes predefined exceptions are not sufficient. In such cases, developers can create their own custom exceptions to represent specific error conditions.
Custom exceptions are user-defined exception classes created by inheriting from the base System.Exception class. They allow developers to:
Output:
Custom Exception Caught: Age must be 18 or above.
Explanation:
Output:
Insufficient balance. Current Balance: 500
Explanation:
| Built-in Exceptions | Custom Exceptions |
|---|---|
| Provided by .NET Framework (e.g., NullReferenceException, IndexOutOfRangeException). | Defined by the user for specific business rules or application needs. |
| General-purpose error handling. | Represents domain-specific error conditions. |
| No extra properties beyond standard exception info. | Can include additional fields and methods. |