How do I handle unchecked exceptions?

I am having difficulty understanding how to handle unchecked exceptions.

If the method does not declare an exception and I am not forced to handle it, how do I know the method is throwing an exception?

Exceptions are thrown for programming errors as I read, so to avoid NullPointerException

I would introduce a null check. Or for ArrayIndexOutOfBoundsException

I would check how I am accessing the array.

But when I don't know that a method is throwing an arbitrary exception, how should I handle it?

How do you propagate exceptions from the data layer to the user interface? Need to check for exceptions, right?

I would allow the data layer to select a checkbox DataLayerExceptions

and then you have a try-catch block in the presenter class (given that I'm using MVP) and then in the catch block sets the error message on the view ..

Or can this be done with thrown exceptions?

EDIT

Thanks for the answers so far.

Here is a specific example I came across.

I am starting with Spring and in the book I saw a class UserRepository

that it uses JDBCTemplate

to retrieve data from a database. UserRepositoty

contains a method like this:

@Override
public List<User> findAll() {
    String sql = "select id, username, email, password from p_user";
    return new ArrayList<>(jdbcTemplate.query(sql, rowMapper));
}

      

The jdbcTemplate request method now throws a DataAccessException

, which is a runtime exception.

I know this because I looked at the method signature.

Now, how would I handle this exception? Of course, the user needs some feedback that something went wrong. And of course, I would log the exception.

But where do I catch this exception?

Do I have to catch it correctly and then throw my own thrown exception? Or should I declare the throws DataAccessException

in the method findAll

and then handle the exception in the calling class (which could be a service or presenter of some kind)?

Or should I make sure that the conditions under which the exception is thrown can never happen (in case they are even controlled by me)?

+3


source to share


3 answers


I am having difficulty understanding how to handle unchecked exceptions.

How easy it is. You handle them the same way as a checked exception. You are using try / catch. (To catch them all, catch RuntimeException

or catch Exception

if you want to check for exceptions as well. But beware of catching Error

or Throwable

.)

What you are really asking is to know when they will be thrown. This is more difficult.

  • As you noticed, some unchecked exceptions are thrown by the language itself. In theory, you can write code to prevent this from happening. In practice: 1) people make mistakes, 2) too many defensive checks are burdensome ... and pointless.

  • Other unchecked exceptions are explicitly declared as thrown

    or documented as being thrown in the javadocs method or constructor. (In fact, I would say that a well-designed API should document the unchecked exceptions it expects to throw or propagate.)

  • In case of problems, you will not know about it until you see an exception at runtime.

The latter case illustrates the need for rigorous and repeatable testing as part of your development cycle.




But when I don't know that a method is throwing an arbitrary exception, how should I handle it?

In general, you don't.

Any unexpected exception should be considered an error. You find it either in testing or in production. You will fix it. If you fixed it correctly, the unexpected exception is not repeated ... or it is expected and your code handles it.

And the corollary is that if you catch and handle an unexpected exception, it is really a bad idea for your code to pretend that nothing bad happened. At the very least, the exception should be written so that the programmer has something to look up with to find the root cause.




How do you propagate exceptions from the data layer to the user interface?

It is generally a bad idea to show exceptions (especially stacktraces!) To users. Typically, you catch them, write them down ... along with the stacktrace ... and then tell the user, "Something bad happened: report it to XXX with the following additional information."

Those should be checked for exceptions, right?

Modulo higher ... No. They don't need to check for exceptions.




I would like the data layer to select the DataLayerExceptions checkbox and then you have a try-catch block in the presenter class (given that I am using MVP) and then in the catch block sets the error message on the view.

You could do it. Think about how to show exceptions to users.

Obviously, if you are going to use this approach, you should be able to distinguish between expected exceptions, which you can explain to the user, handle, and “continue” versus unexpected exceptions that cannot be explained and cannot be recovered from. You may have some kind of circuit for this ...




There is no simple formula or "best practice" on the bottom line that will solve this problem for you. It takes thought and design ... and care ... and TESTING.

+3


source


When you write small "learning" applications; then it is perfectly okay to just throw out unchecked bubble exceptions before the main method; and even this stack trace will be printed to you on the command line.

In a "real world" application; you would at some level guarantee that something will catch such exceptions; do things like:

  • presents a meaningful error message to the user
  • create some log entry for later debugging
  • decide if the application can continue; or should "crash" (perhaps something that rarely happens in the real world).


In other words: One important non-functional requirement of "real world" applications is reliability . You don't want a missing null check somewhere to break your whole application. Thus, you must make sure that your application can continue to work in such situations. (and please note: I am not saying that this kind of thing is easy to implement).

In addition: can use its own checked exception; but the big drawback is there: they messed up your method signatures. But this is a very stubborn discussion (and although some important people argue: "the proven war against the out of control is over, and out of control gain"); the official Oracle documentation still assumes that checked exceptions have their place in the real world.

+2


source


As a rule, you shouldn't catch the blues that are completely unexpected RuntimeExceptions

. That would be ugly (your code would be filled with try-catch statements) and there is no universally graceful way to recover from them.

Many of these RuntimeExceptions will be thrown due to programming errors that you must fix when testing your application. Fixing the underlying error causing, say, NullPointerException

beats any catch and fix that the calling code can do.

Sometimes APIs will throw RuntimeExceptions

(read their documentation) which you expect to catch. For example, checking the parameters may result in an outlier IllegalArgumentException

. In such cases, it might be prudent to catch those specific exceptions and handle them gracefully (for example, by re-throwing them as a custom thrown exception, remember to use the parameter cause

when re-throwing). In the case of these, IllegalArguments

you can mark the user's login as invalid and request correction. Good APIs will detail what is being thrown when. Read the case of checked exceptions to understand why any API programmer would never want to throw unchecked rather than checked exceptions.

However, as was found in Ghostcat's (excellent) answer, if the application will have users (besides programming exercises), the implicit catch-all-and-show-stacktraces in the main()

. They don't make sense to non-developers, and don't provide enough context for actual developers unless they know what's going on before they are thrown out. This is good practice for

  • write down any exceptions that fail validation so that you can later figure out what happened. You should already be using logging to help you understand what was happening before the exception was thrown.

  • provide helpful feedback to the users who run them so they can do something about it. Note that "useful" is user dependent; for an e-commerce web application, only the developers will consider the exclusive details information; while buyers will be better served by a friendly, low verbosity error message, basically asking them to try again later.

It's a bad idea, instead of the above two, to wrap all unhandled exceptions with a custom checked exception. It doesn't fix anything: you still need to follow the above two steps to try to fix the situation later.

+1


source







All Articles