By Fatskills Exam Guides Team — the exam nerds behind 28,500+ quizzes and 2.1M practice questions across 500+ global exams.
Custom exceptions in C# allow developers to create specific error types that provide more meaningful information than generic exceptions. This is crucial for debugging and maintaining robust applications. In real-world scenarios, custom exceptions help in identifying and handling specific error conditions, leading to more maintainable and understandable code. If you get this wrong, your application may become difficult to debug, and errors may go unnoticed or be misinterpreted, leading to potential crashes or incorrect behavior.
Example: ```csharp public class CustomException : Exception { public CustomException() { }
public CustomException(string message) : base(message) { } public CustomException(string message, Exception inner) : base(message, inner) { } protected CustomException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
} ``` - ⚠️ Pitfall: Not providing constructors that match the base Exception class can lead to incompatibility issues.
Throw the Custom Exception
csharp public void ValidateInput(int value) { if (value < 0) { throw new CustomException("Value cannot be negative."); } }
⚠️ Pitfall: Throwing exceptions for non-error conditions can lead to performance issues and confusing code.
Catch the Custom Exception
csharp try { ValidateInput(-1); } catch (CustomException ex) { Console.WriteLine(ex.Message); }
Experts view custom exceptions as a way to make error handling more intuitive and maintainable. They think about the semantics of the error conditions and design custom exceptions that clearly communicate the nature of the problem. This approach helps in creating self-documenting code and makes it easier for other developers to understand and maintain the application.
Exam trap: Questions that require handling serialized exceptions.
The mistake: Throwing exceptions for non-error conditions.
Exam trap: Scenarios that involve performance optimization.
The mistake: Catching all exceptions with a generic catch block.
Exam trap: Questions that require identifying the source of an error.
The mistake: Not including meaningful error messages.
Scenario: You are developing a banking application that requires validating user inputs for transactions.Question: How would you create and use a custom exception to handle invalid transaction amounts? Solution: 1. Define the custom exception class: ```csharp public class InvalidTransactionAmountException : Exception { public InvalidTransactionAmountException() { }
public InvalidTransactionAmountException(string message) : base(message) { } public InvalidTransactionAmountException(string message, Exception inner) : base(message, inner) { } protected InvalidTransactionAmountException( System.Runtime.Serialization.SerializationInfo info, System.Runtime.Serialization.StreamingContext context) : base(info, context) { }
} 2. Throw the custom exception in the validation method:csharp public void ValidateTransactionAmount(decimal amount) { if (amount <= 0) { throw new InvalidTransactionAmountException("Transaction amount must be greater than zero."); } } 3. Catch the custom exception in the calling code:csharp try { ValidateTransactionAmount(-10); } catch (InvalidTransactionAmountException ex) { Console.WriteLine(ex.Message); } ``` Answer: The custom exception InvalidTransactionAmountException is thrown and caught, displaying the message "Transaction amount must be greater than zero." Why it works: This approach provides a clear and specific way to handle invalid transaction amounts, making the code more maintainable and understandable.
2. Throw the custom exception in the validation method:
3. Catch the custom exception in the calling code:
public class CustomException : Exception { ... }
Join 4M+ learners. Unlock unlimited quizzes, wrong-answer tracking, flashcards + reminders, study guides, and 1-on-1 challenges.