Alex Rivera | Logout

When is it OK to catch a RuntimeException

Asked 2009-12-30T21:13:41.367
82

On a recent project I recommended catching a RuntimeException within a test harness code and logging it. The code processes a series of inputs from a database, and I do not want the test to stop due to failure of any one input (Null values, Illegal arguments, etc.). Needless to say, my suggestion triggered a passionate discussion.

Is catching any kind of RuntimeException acceptable? If yes, what are other scenarios where it is OK to catch RuntimeExceptions?

Edit
Report

1 Answer

11

In my code 99% of my exceptions are derived from runtime_exception.

The reasons I catch exceptions are:

  • Catch Log and Fix problem.
  • Catch Log and Generate a more specific exception and throw
  • Catch Log and rethrow.
  • Catch Log and Kill operation (discard exception)
    • User/request initiated action fails.
      An HTTP request handler for example. I would rather the requested operation die rather than bring the Service down. (Though preferably the handler has enough sense to return a 500 error code.)
    • Test case passed/failed with an exception.
    • All exceptions not in the main thread.
      Allowing exceptions to escape a thread is usually badly documented but usually causes program termination (without stack unwinding).
answered 2009-12-30T21:27:24.960

Your Answer