Where do you like to catch exceptions and why?

前端 未结 9 1476
别跟我提以往
别跟我提以往 2020-12-03 08:31

Where do you like to catch exceptions and why?

I\'m interested in seeing where people find it useful to put their try/catch blocks in the hope that some general patt

9条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-03 09:10

    In a Delphi Windows Application, the main message handling loop for your application (i.e. at the bottom of the call stack) handles your exceptions by showing a message box. In my opinion this is the best place to handle exceptions.

    By catching exceptions in your own methods for the sole purpose of showing a message box to the user, you're denying any code calling your method of knowing that an exception actually occured and that the method infact failed.

    I would only handle an exception if:

    • I need to do some cleanup (like a DB rollback) in which case I would reraise the exception after the cleanup is done.
    • I have some more information to add to the exception.
    • My method can succeed in it's purpose despite the exception.

提交回复
热议问题