Back to course

Custom Exceptions and Throwing

.NET Zero to Hero: Master C# and Modern App Development

Throwing Exceptions

You can manually throw an error using the throw keyword.

Example:

csharp static void CheckAge(int age) { if (age < 18) { throw new ArithmeticException("Access denied - You must be at least 18 years old."); } }

You can also create your own Exception classes by inheriting from Exception.