Junior — Middle
Can you tell about your experience using custom exceptions in real projects?
sobes.tech AI
Answer from AI
In one of my C# projects, I created custom exceptions for more precise error handling related to business logic. For example, to validate user data, I defined a ValidationException class that inherits from Exception. This allowed centralized validation error handling and providing clear messages to the user.
Example:
public class ValidationException : Exception
{
public ValidationException(string message) : base(message) { }
}
// Usage
if (string.IsNullOrEmpty(userName))
{
throw new ValidationException("Username cannot be empty.");
}