Exception Handling – Where to Handle Fatal Exceptions

exception handlingexceptions

Suppose I have a controller that loads a file and hands it over to the processing.

  • Should I handle the exception in the file loader and return Null if something is wrong, or should I throw the exception and handle it in the controller?

  • Without the file the rest of the program canĀ“t work. Where should I handle a exception that shuts down the program properly?

I want to shut down an Android application properly.

Best Answer

In layman's words:

  • You should not return null.
  • The exception should be handled in the top-most layer.
  • The program should not terminate, but give feedback to the user, unless it is a Unix style command line program where it's ok to terminate after showing an error message.
  • By definition no exception is fatal and each one can be handled.
  • Errors are fatal and cannot be recovered from.
  • Exceptions and errors are not the same.
Related Topic