Is it good practice to have specific exceptions for everything? Or is it better to repeat a slightly more abstract exception?

I am dealing with a project in which I wrote one exception for each possible exception situation. The fact is that I find it more “readable,” but I get a crazy amount of different exceptions.

Is it considered good practice to do so? Or should I write only exceptions, more abstract, so as not to have much?

Thanks so much for your time.

+6
java exception-handling
source share
6 answers

It better depends on the likelihood that your code will catch certain exceptions. If you can only ever catch (or distinguish in some other way) more general (superclasses) exceptions, then having a lot of more specific (subclasses) exceptions does not achieve much. In this case, it is probably better to identify fewer exceptions and use exception messages to express more subtle details of what went wrong.

On the other hand, if specific exceptions already exist, it makes sense to use them. Just throwing java.lang.Exception or java.lang.RuntimeException is lazy, IMO.

FOLLOW UP

Well, I always find special exceptions, but the fact is that in other "catches" I also use certain exceptions that are similar (for example, they can refer to the "database", but they are not the same), so the question is that it would be nice to throw a "DatabaseException" and use it instead of "DatabaseConnectionException" and "DatabaseDataException", for example, more readable, but in the end I got millions of explicit exceptions.

If your code often looks like this:

 try { ... } catch (DatabaseConnectionException ex) { // do something } catch (DatabaseDataException ex) { // do same thing } catch (DatabaseTangoException ex) { // do same thing } 

... then your fine-grained exceptions do not help. But if it looks like this:

 try { ... } catch (DatabaseConnectionException ex) { // do something } catch (DatabaseDataException ex) { // do something completely different } catch (DatabaseTangoException ex) { // do something totally extraordinary } 

... then maybe your minor exceptions will work for you. And if you declare these three exceptions as subclasses of DatabaseDataException , then you can handle cases together or separately, as circumstances require.

In fact, you decide what to do in the context of your application.

+4
source share

I suggest having an abstract domain exception (where the domain reflects your organization or the level of the application in which the problem occurs) that extends to all of your specific exceptions.

Then you can just catch a DomainException to say that this is a problem with your code, and then check if necessary.

+4
source share

Bloch, J., 2008. Effective Java. 2nd ed :

Paragraph 60: Use standard exceptions

Reusing preexisting exceptions has several advantages. The main one, it makes it easier to learn your API and use, since it complies with established conventions with which programmers are already familiar. The next second is that programs using your API are easier to read because they arent cluttered with unfamiliar exceptions. The last (and least) less exception classes mean less memory space and less time to load classes.

+3
source share

Just because you can extend exceptions doesn't mean you should. Dogbane's answer provides good reasons for using standard exceptions. (Note that it says using "standard" exceptions, not "general"! Use as a specific standard exception that you can find.)

I believe that you should use your own subclass of exceptions only when both of these conditions are true:

  • You want to do something specific when you catch a certain category of exceptions; using a specific subclass in this case allows you to catch it, allowing other exceptions to be thrown without problems.
  • No standard exception covers your category with sufficient accuracy.
+1
source share

Why not just create a small number of exceptions and let them take a descriptive line in the constructor?

 throw new GenericException("Error: problem with...."); 

And let the toString () method print the string you passed.

0
source share

I consider it a good practice to have special exceptions when possible.

Following this practice, we provide various error messages based on the type of exception.

Even programming languages ​​such as Java also support this concept, allowing programmers to use the extend Exception class and create their own Exception subclasses.

You can get more information on the following subject. Exception Handling Question

0
source share

All Articles