Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What factors should be taken into consideration when writing a custom exception class?

Tags:

oop

exception

When are custom Exception classes most-valuable?
Are there cases when they should or should not be used? What are the benefits?

Related questions:

  1. Performace Considerations for throwing Exceptions
  2. Do you write exceptions for specific issues or general exceptions?
like image 260
keparo Avatar asked Oct 12 '08 18:10

keparo


People also ask

What are some of the points you should consider before creating custom exceptions?

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.

What is a custom exception class?

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.

What is the advantage of creating your own custom exception class?

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.


2 Answers

Questions to ask yourself:

  1. Who will be catching it? If no one, then you don't really need a custom exception.
  2. Where will you be throwing it? Is there enough context readily available, or will you need to catch and re-throw several times before the exception is useful to the final catcher?
  3. Why are you throwing it? Because you caught an exception and need to attach additional information? Because you encountered an unrecoverable error in some data, and need to communicate the specifics back to client code? Because you like throwing things?
  4. What is the exception? Not what caused it but what is it from the perspective of the catcher? Something they can fix and retry? Something they should never retry? Something they should notify the user about? Something they should attach context information to and then re-throw? What determines the information you'll need to pass along, if any...

Precepts:

  1. Do not waste time on custom exceptions that will never be caught.
  2. Do not "double up" exceptions: each custom exception type should have a well-defined case where it can and should be caught; exceptions that don't match should be broken out into their own custom types (rather than, say, forcing the catcher to build conditional logic into a single catch() clause).
  3. Unless you have a good reason not to, always allow attaching an inner exception / data from a previously-caught exception. Losing context is rarely helpful.
like image 82
Shog9 Avatar answered Nov 11 '22 06:11

Shog9


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

like image 26
Wheelie Avatar answered Nov 11 '22 07:11

Wheelie