9

In my company we are building a webapp containing serveral central services that we design ourselves and then specify as interfaces. I.e. the interfaces are application specific and they are then implemented with third party libraries which we may change over time. When it comes to exceptions, I have realized that the exceptions thrown by our services should be our own application-specific exceptions as opposed to implementation specific exceptions.

Now, I am wondering how our exceptions should be structured and related to each other.

First, consider a generic exception MyAppException. This exception indicates that something unexcpected went wrong and the best thing we can do is to display a message to the user saying that something went wrong and that we are working on it. The error could be that the database crashed or something similair. This exception would pretty much be thrown from all methods working with the database.

Secondly, consider an exception MyAppDuplicateException. This exception would indicate that the user tried to save something into the database that was already there. Here, we can display a much more specific error message and this exception is only thrown from the methods that inserts or updates database rows.

The application can also contain other exceptions similar to MyAppDuplicateException for other excpected error situations. Ex MyAppNotFoundException etc...

Now to my questions:

  1. Should the other exceptions extend MyAppException? I actually see no reason for this, I have just seen it in a lot of places and wonder if there is a purpose to it. The downside of this as I see it is that a try/catch-statement does not need to care about the specific exception in this case. It can just catch the top exception and because of this it does not need to handle the specific error which was pretty much the point of having the specific exception.
  2. If the other exceptions does not extend MyAppException, should MyAppException be a java.lang.RuntimeException? This would not require executing code to catch it, which to me sounds natural since the point of the exception is to say that something unknown happened and the executing code is not excpected to be able to handle it. The code at the entry point of the request could still have a try/catch statement that catches MyAppException and makes sure that a message is displayed to the user.

edit There is no question if the specific exceptions like MyAppDuplicateException should be checked or not, the should defenetly be checked.

2 Answers 2

9
  1. Sometimes you want to catch a specific type of exception (e.g. MyAppDuplicateException) and sometimes you want to catch a whole category of exceptions (e.g. MyAppException). By having MyAppDuplicateException extend MyAppException you're giving the calling code a bit more flexibility in how it deals with the different exceptions.
  2. The best advice I've heard on this is that generally speaking you should throw a checked exception if whatever called your method obeyed its contract. It's a way of saying "here are the things that might be expected to fail". I would definitely make MyAppDuplicateException and the like checked exceptions (i.e. not RuntimeExceptions).

There's a very good article here which should help you avoid most of the common pitfalls.

4
  • I see your point with nr 1. However it is my excperience that we never encounter the situation where we want to catch all types of exceptions from our services. If something goes wrong, we really want to know excatly what it is. What I hear from you second point is that MyAppException should be RuntimmeException. (the others should be checked, there is no question about that) Commented May 29, 2012 at 14:11
  • @vaughandroid, the link is broken. Here is the archive: community.oracle.com/docs/DOC-983543
    – user167569
    Commented May 20, 2018 at 10:52
  • @user167569 Thanks for the heads up. Have fixed the link. Commented May 21, 2018 at 7:46
  • @LudwigMagnusson #1 shows the issue when you want to catch a branch of exceptions at a certain point in time, possibly after handling more specific ones. That's not the same as catching all exceptions. The problem with #2 is context. You could say that an IOException should be a runtime exception for File not Found if the file is a resource that should be there. It is however a checked exception in the case that a user requests a specific file name that may or may not exist. This is especially true when creating libraries for which the context is not known in advance. Commented Nov 21, 2018 at 23:14
1

I have realized that the exceptions thrown by our services should be our own application-specific exceptions as opposed to implementation specific exceptions.

By this you mean exceptions defined by your third-party implementation? Correct?

First, consider a generic exception MyAppException. This exception indicates >that something unexcpected went wrong and the best thing we can do is to >display a message to the user saying that something went wrong and that we are >working on it. The error could be that the database crashed or something >similair. This exception would pretty much be thrown from all methods working >with the database.

If this kind of exception represents an error in your program that is not caused by bad user input but rather some problem with your code, then in Java it should be a runtime exception.

Secondly, consider an exception MyAppDuplicateException. This exception would >indicate that the user tried to save something into the database that was >already there. Here, we can display a much more specific error message and this >exception is only thrown from the methods that inserts or updates database >rows.

This is a checked exception in Java.

If the other exceptions does not extend MyAppException, should MyAppException >be a java.lang.RuntimeException?

Yes.

This would not require executing code to catch it, which to me sounds natural >since the point of the exception is to say that something unknown happened and >the executing code is not excpected to be able to handle it. The code at the >entry point of the request could still have a try/catch statement that catches >MyAppException and makes sure that a message is displayed to the user.

Yes that's right.

2
  • You understand me correctly. Commented May 29, 2012 at 14:14
  • When you say that the error is caused by "a problem with my code" I just want to point out that the problem could also be with the database connection etc. Not just errors in programming. Commented May 29, 2012 at 14:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.