How to handle exception in catch block?

前端 未结 3 1915
半阙折子戏
半阙折子戏 2021-01-24 03:33

I am trying to get the ideal way to handle exception. I googled & read that I should put try catch in the catch block as well to handl

3条回答
  •  渐次进展
    2021-01-24 03:41

    I would go with the comment of Tieson T. . From my point of view it is an design issue.

    I could also build an example with if statements -> if that goes wrong, I perform failure handling -> if the failure handling goes wrong, I perform failure handling, If the failure handling goes wrong ....

    To make the code more readable, you can try to "hide" the try-catch blocks in method like:

    static void PerformInTryCatch(Action action, T obj) where TException : Exception
        {
            try
            {
                action(obj);
            }
            catch (TException exception)
            {
                // Perform some logging   
            }
        }
    

    Hope that helps.

提交回复
热议问题