When is it OK to catch a RuntimeException

前端 未结 10 1094
眼角桃花
眼角桃花 2020-12-07 11:03

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

10条回答
  •  感情败类
    2020-12-07 11:54

    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).

提交回复
热议问题