When are custom Exception classes most-valuable?
Are there cases when they should or should not be used? What are the benefits?
Related questions:
Three steps to create and use custom exception class (1) Define exception class (2) Declare exception prone method with throws keyword (3) Check condition to throw new exception object to be handled by calling the method.
The purpose of a custom exception class is to integrate the look-up of localized message strings in a custom message catalog into the mechanism that is used for error reporting in the client infrastructure.
The big advantage is that it allows you to throw and exceptions that mean what you want them to mean. If you reuse an existing exception, any piece of your code that catches the exception has to deal with possibility that the actual exception wasn't thrown by your code, but by some other library party code.
throw
ing things?catch()
clause).I'm a bit of a minimalist and will only create a custom exception if there is calling code that must explicitly react to the particular condition that occured. For all other situations I'll use the most appropriate .NET library exception. E.g. ArgumentNullException, InvalidOperationException
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With