Multiple try-catch or one?

前端 未结 11 1731

Normally, I\'d do this:

try
{
    code

    code that might throw an anticipated exception you want to handle

    code

    code that might throw an anticip         


        
11条回答
  •  借酒劲吻你
    2020-12-08 09:46

    It depends on the nature of the type of errors happen in your code.

    1. If the handling of those errors you are going to do is same go for single try ... catch for that group of code. Else it will be too tedious.

    2. If the errors required different handling, separate it because you have to.

    DO NOT APPLY A SINGLE METHOD FOR ALL CASES. PROGRAMMING MOST OF THE TIME IS CONTEXT SPECIFIC :)

    You need to reach a balance of "GOOD/COMPLEX" and "BAD/SIMPLE". The more you code, you will be less stuck in this kind of dilemma :)

    Happy programming!

提交回复
热议问题