Try Catch blocks inside or outside of functions and error handing

前端 未结 6 629
野性不改
野性不改 2020-12-30 21:35

This is more a general purpose programming question than language specific. I\'ve seen several appraoches to try and catches.

One is you do whatever preprocessing on

6条回答
  •  情歌与酒
    2020-12-30 22:21

    There are no real hard and fast rules around exception handling that I have encountered, however I have a number of general rules of thumb that I like to apply.

    Even if some exceptions are handled at the lower layer of your system make sure there is a catch all exception handler at the entry point of your system (e.g. When you implement a new Thread (i.e. Runnable), Servlet, MessasgeDrivenBean, server socket etc). This is often the best place to make the final decision as how your system should continue (log and retry, exit with error, roll back transaction)

    Never throw an execption within a finally block, you will lose the original exception and mask the real problem with an unimportant error.

    Apart from that it depends on the function that you are implementing. Are you in a loop, should the rest of the items be retried or the whole list aborted?

    If you rethrow an exception avoid logging as it will just add noise to your logs.

提交回复
热议问题