1

Recently I was at an interview and the interviewer asked me regarding exception handling. I gave the typical checked unchecked answer. Then He asked me should you handle runtime exception ? I said no and then it was asked that if we get a NullPointerException suppose, will you send the same to the user ?

I got confused. So what I am looking for is that, should we catch runtime exceptions or not ?

  1. If Yes, How can we do that ?
  2. If No, then do we show java.lang.nullpointerException to the user ?

Thanks in advance.

  • 1
    you should if you don't want the application to crash. I recommend you read up the basics of Exception Handling – Stultuske Nov 24 '17 at 09:21
  • Check [here](https://stackoverflow.com/questions/25696642/handling-unchecked-exceptions-globally-in-project/25696687#25696687) – Procrastinator Nov 24 '17 at 09:36

3 Answers3

3

The best way to handle any predictable RuntimeException is to catch it and produce user-friendly information about what happened with probable causes and what the system expects the user to do next.

Suppose if you have a project that provides the user with a UI based calculator. Now, what should ideally happen if the user enters a 'division with zero' instruction? Say if you are not checking for the 0 denominator (which of course is not advisable), your program will throw an ArithmeticException. How should you handle it? You should catch it and let the user know that he has entered an input which the system was not expecting, i.e.

You cannot divide by zero. Please enter a non zero denominator!

If it is an API and is to be consumed by another application, what I would do is to wrap the runtime exception into a customized API specific exception and throw it from there, so that the implementor can use the information and handle it in a suitable fashion. e.g.

public int divide() throws InvalidArgumentsException
Yash
  • 11,486
  • 4
  • 19
  • 35
0

Your average user isn't going to know what the NullPointerException is or how/why it happened. You would usually present the user with a more friendly error, perhaps with an error code they can provide to help identify the error.

The application may also save the NullPointerError along with the stack trace to a file so a developer can see exactly where the error occured.

Matt Clegg
  • 359
  • 1
  • 4
  • 17
  • Exactly, I need not to show the user that NPE occurred. But how to show the user the friendly error ? NPE is just an example. There can be N numbers of runtime exception. SO how can I go about implementing the same in the code ? – IllegalSkillsException Nov 24 '17 at 09:44
0

You can catch RuntimeExceptionclasses in your Application by implementing a UncaughtExceptionHandler:

Thread.setDefaultUncaughtExceptionHandler(Thread t, Throwable e) -> {

    // log Exception to file, send to server ecc, you can check which exception was thrown and act accordingly...

    // you can still re-throw the exception here as if this block did not exist 

});

This method should catch all uncaught Exceptions, even those that were thrown by other Thread instances, so it's safe to just use one implementation.


You question:

should we catch runtime exceptions or not

It depends; you don't want a whole Banking Application Server crash, just because it could not find a file in the file-system.

At the same time, RuntimeException's usually happen because there is a flaw in the code. NullPointerException's can be avoided, as well as many others...

payloc91
  • 3,724
  • 1
  • 17
  • 45