Creating Custom Exceptions
When you need to handle errors that don't fit the standard exceptions provided by C#, you can create your own custom exception classes. Custom exceptions allow you to make your error handling more descriptive and specific to your application's needs. You should create a custom exception when the built-in exceptions don't clearly communicate the problem, or when you want to signal a unique error that only your application can recognize.
A custom exception is a user-defined class that extends the standard exception hierarchy. By creating your own exception types, you make your error handling clearer and tailor it to the needs of your application. This improves code readability and helps others understand what went wrong and why.
Program.cs
123456789101112131415161718192021222324252627282930313233343536373839404142using System; namespace ConsoleApp { // Define a custom exception for invalid age public class InvalidAgeException : Exception { public InvalidAgeException(string message) : base(message) { } } public class Validator { public void ValidateAge(int age) { if (age < 0 || age > 130) { throw new InvalidAgeException("Age must be between 0 and 130."); } Console.WriteLine("Age is valid."); } } public class Program { public static void Main(string[] args) { Validator validator = new Validator(); try { validator.ValidateAge(150); } catch (InvalidAgeException ex) { Console.WriteLine("Custom exception caught: " + ex.Message); } } } }
A custom exception class usually inherits from the Exception base class. This allows your exception to be used in try-catch blocks like any other exception. In the code above, InvalidAgeException is defined with a constructor that accepts a message, which is passed to the base Exception class. You use this custom exception by throwing it with the throw keyword when a specific error condition occurs, such as an invalid age.
Custom exception classes can be enhanced with extra information about the error. You might want to include additional properties to give more context.
CustomExceptionWithProperty.cs
123456789101112131415using System; namespace ConsoleApp { // Custom exception with an additional property public class InvalidAgeException : Exception { public int InvalidAge { get; } public InvalidAgeException(string message, int invalidAge) : base(message) { InvalidAge = invalidAge; } } }
1. What is the main advantage of creating custom exceptions?
2. How do you define a custom exception in C#?
Merci pour vos commentaires !
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Can you show me an example of a custom exception in C#?
When should I use a custom exception instead of a built-in one?
How do I add extra information to my custom exception class?
Génial!
Completion taux amélioré à 4.17
Creating Custom Exceptions
Glissez pour afficher le menu
When you need to handle errors that don't fit the standard exceptions provided by C#, you can create your own custom exception classes. Custom exceptions allow you to make your error handling more descriptive and specific to your application's needs. You should create a custom exception when the built-in exceptions don't clearly communicate the problem, or when you want to signal a unique error that only your application can recognize.
A custom exception is a user-defined class that extends the standard exception hierarchy. By creating your own exception types, you make your error handling clearer and tailor it to the needs of your application. This improves code readability and helps others understand what went wrong and why.
Program.cs
123456789101112131415161718192021222324252627282930313233343536373839404142using System; namespace ConsoleApp { // Define a custom exception for invalid age public class InvalidAgeException : Exception { public InvalidAgeException(string message) : base(message) { } } public class Validator { public void ValidateAge(int age) { if (age < 0 || age > 130) { throw new InvalidAgeException("Age must be between 0 and 130."); } Console.WriteLine("Age is valid."); } } public class Program { public static void Main(string[] args) { Validator validator = new Validator(); try { validator.ValidateAge(150); } catch (InvalidAgeException ex) { Console.WriteLine("Custom exception caught: " + ex.Message); } } } }
A custom exception class usually inherits from the Exception base class. This allows your exception to be used in try-catch blocks like any other exception. In the code above, InvalidAgeException is defined with a constructor that accepts a message, which is passed to the base Exception class. You use this custom exception by throwing it with the throw keyword when a specific error condition occurs, such as an invalid age.
Custom exception classes can be enhanced with extra information about the error. You might want to include additional properties to give more context.
CustomExceptionWithProperty.cs
123456789101112131415using System; namespace ConsoleApp { // Custom exception with an additional property public class InvalidAgeException : Exception { public int InvalidAge { get; } public InvalidAgeException(string message, int invalidAge) : base(message) { InvalidAge = invalidAge; } } }
1. What is the main advantage of creating custom exceptions?
2. How do you define a custom exception in C#?
Merci pour vos commentaires !