Using RuntimeExceptions and CheckedExceptions

What do you view when using CheckedExceptions and RuntimeExceptions in an application? I was encouraged to use a combination of both and, as I understand it, you can have a chain of CheckedException calls distributed along with a RuntimeException.

+4
source share
7 answers

Checked exceptions should only be sent if you can reasonably expect the caller to process them. Otherwise, throw a RuntimeException (which does not require you to declare it or that the handler caught it. This is the approach that Spring JDBC takes).

Read more from Sun here .

If the client can reasonably expect recovery from the exception, make it a verified exception. If the client cannot do anything to recover from the exception, make it an uncommitted exception.

+14
source

I agree with Joel.

Take a look at the error pattern: link text

And remember that a checked exception should not reveal the internal mechanism of your methods.

+4
source

in my opinion, checked exceptions are a flaw in the java design (for example, C # seems to have learned from it and offers only unverified exceptions). Sun says you should use checked exceptions if clients can recover. The problem is that, as an api developer, you just don't know which clients you can expect (-> can they recover or not?). Therefore, you should not correlate this uncertainty with your api. Therefore, I would go for excepted exceptions.

In my opinion, when you think of “reasonable client manipulations”, it often happens that in the end you pollute your api. IOException and RemoteException are notable examples of "annoying checked exceptions." In practice, they most of the time do not make sense to process clients. Of course, in rare cases, they make sense to process. Therefore, looking at this, I try to develop an api client interface and avoid the burden of checked exceptions. Of course, when using excluded exceptions, they should be documented (e.g. via javadoc).

Due to the reduced compatibility of Java, most likely checked / unverified will never change.

For more details and specific examples, perhaps take a look at the blog post I made some time ago: Getting rid of checked exceptions

However, I sometimes still work with checked exceptions for several reasons:

  • The existing code base is large and uses checked exceptions. The refactoring efforts for excluded exceptions are too high given the benefits.
  • Most team members believe that the checked exception is somewhat good, and we agreed on democracy at several conventions.
+2
source

Checked exceptions are what you expect and can handle - like an IO exception exception or a database connection. In addition, a user-created exception also falls into the test exception.

Runtime errors or unchecked exceptions are what you do not expect - they arise due to logical errors in the code, for example, an arrayindexoutofbounds exception, an exception from a null pointer.

And to think about it as a programmer, you create code that will work well and sound logical, and you will not expect logical errors. And, if they were, the JVM catches and issues the system.

Hope this helps.

+1
source
  • You should use unchecked exceptions when these are errors in your own code and the application cannot reasonably recover from errors, that is, errors.
  • Use checked exceptions for errors outside the control of your applications, for example, bad user and database input errors.
0
source

Checked exceptions are expected exceptions. Or you can throw or catch checked exceptions. Catching it, the recovery from the exception is performed by the class itself. Throwing checked exceptions, you throw exceptions outside the class and allow the outsider to handle the exception.

Exceptions due to an error in the program arise due to an error. If a class raises an exception at runtime, you cannot exclude another class that takes care of this exception. therefore, as a rule, runtime exceptions are not thrown.

0
source

All Articles