Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom Exceptions: Differentiate via many subclasses or single class backed with enum?

I'm looking to implement my own set of Exceptions for a projects I am currently working on. The project relies on a core framework with a base framework exception MyFrameworkException (I am also writing this framework).

For any given project I would like to throw several different types of Exceptions and I can't decide between using multiple subclasses or a single subclass with some form of an Enum as a constructor parameter.

In both cases I have:

public class MyFrameworkException   extends Exception              { /*...*/ }

Option 1:

public class MyProjectBaseException extends MyFrameworkException   { /*...*/ }
public class SpecificExceptionType1 extends MyProjectBaseException { /*...*/ }
public class SpecificExceptionType1 extends MyProjectBaseException { /*...*/ }
public class SpecificExceptionType1 extends MyProjectBaseException { /*...*/ }

Then, throughout the project I would throw the specific exception for any problem that occurs.

Option 2:

public class MyProjectException extends MyFrameworkException {
  public static enum Type {
    SpecificType1, SpecificType2, SpecificType3
  }
  public MyProjectException( Type type ) { /*...*/ }
}

Here I would always throw MyProjectException with the specific enum type for any problem that occurs. I'd provide some mechanism so that a switch statement could be performed on any MyProjectException based on the type enum.

What's the best way to handle exceptions in projects, especially those sharing a common infrastructure? Are the two options above good solutions? Why or why not? And what are any better solutions?

like image 212
Andy Avatar asked Oct 14 '10 15:10

Andy


People also ask

What are custom exceptions?

In simple words, we can say that a User-Defined Exception or custom exception is creating your own exception class and throwing that exception using the 'throw' keyword. For example, MyException in the below code extends the Exception class.

What is difference between enum and class in Java?

An enum can, just like a class , have attributes and methods. The only difference is that enum constants are public , static and final (unchangeable - cannot be overridden). An enum cannot be used to create objects, and it cannot extend other classes (but it can implement interfaces).

How do you create multiple custom exceptions in Java?

Example: public myfunction throws invalidEmpIDException ,invalidPermissionEception, MyExceptionTHREE { // My Code here.. // On some condition throws invalidPermissionException, // on Some condition throws invalidEmpIDException, // on Some cndition throws MyExceptionTHREE.. }

What is custom exception class in Salesforce?

Custom exceptions enable you to specify detailed error messages and have more custom error handling in your catch blocks.


2 Answers

The chief disadvantage to Option 2 (a common exception + enum) is that you lose some of the utility of checked exceptions. A method pretty much has to say simply "something framework-related might go wrong":

public void foo()
throws MyFrameworkException

...rather than "x or y might go wrong":

public void foo()
throws SomethingWentWrongException, SomethingElseWentWrongException

It means a function that may need to handle one framework exception has to be prepared to handle any of them, whereas if you're specific, a function need only be prepared to handle the exceptions that are thrown by the framework methods it calls.

So for me, a hierarchy such as Option 1 (it needn't be quite so flat, if a structure suggests itself) is the way to go. That said, there are people who don't like checked exceptions at all, and for them I suspect the above is not a compelling argument. :-)

Edit And picking up duffymo's point: I've assumed you were talking about exceptions you really did have to create. Absolutely throw standard exceptions wherever it makes sense (which is nearly everywhere). Don't create your own MyFrameworkIllegalArgumentException, for instance, just use IllegalArgumentException (or its various subclasses).

like image 105
T.J. Crowder Avatar answered Oct 20 '22 00:10

T.J. Crowder


I would go with exceptions extending java.lang.RuntimeException with descriptive class names.

If you have so many business-specific exceptions that this becomes oppressive, it means you're probably doing it wrong.

See Joshua Bloch's advice about favoring standard exceptions.

like image 39
duffymo Avatar answered Oct 20 '22 00:10

duffymo