How many statements in a try/catch statement?

前端 未结 13 1250
予麋鹿
予麋鹿 2021-01-12 12:42

Should I put multiple statements in a try and then catch all possible exceptions, or should I put only one statement in the try statement?

Example:



        
相关标签:
13条回答
  • 2021-01-12 13:20

    If the method you are calling can return FooExeption() and BarException() and you want to catch both, then the first example make the most sense - quite a few API's do this, particularly ones that are higher level (as they themselves are using more things which can raise exceptions).

    If you are doing two separate things then it really entirely depends on what you want the outcome to be:

    • If an exception in either ultimately amount to the same thing as far as your code is concerned, and you don't care about having roll anything back (or it's simple to roll back from either and you can easily do it in a single finally block - assuming the language supports this) then there is no point in having two separate try/catch blocks.

    • If the error types are very varied and you care what happens if the first method raises an exception (e.g. and need to do some operations to roll back) and you want to continue executing the second operation, even if the first one threw an exception, then the second approach would be more appropriate.

    • If you care if the first method fails and you don't want to continue executing if it does, then it's worth remembering that you can nest try/catch, though it's best not to go overboard with this. If used well it's much clearer than trying to follow bools in if statements to check if a block should execute or not.

    e.g.

    try {
    
        MaybeThrowFooException();
    
        try {
        // Will only be called as long as MaybeThrowFooException() is not thrown
            MaybeThrowBarException();
    
        } catch (BarException ex) {
    
        }
    
    } catch (FooException ex) {
    
    }
    
    0 讨论(0)
  • 2021-01-12 13:22

    It depends of the case , but it's important to notice that in the first case MaybeThrowFooBarException() is nerver called if MaybeThrowIOException() throws an exception, and in the second case MaybeThrowFooBarException will be allways called unless a exception is rethrown in the first catch

    0 讨论(0)
  • 2021-01-12 13:24

    You can handle multiple types of exceptions through single try / catch loop. But take care of order in which you are going to handle exceptions. Order of catch exception block does matter.

    0 讨论(0)
  • 2021-01-12 13:28

    I think the best practice is the one detailed in the book The Pragmatic Programmer, exceptions should rarely be used - but when being used it should clear to what it's suppose to be handling.

    So, my vote is example #2.

    0 讨论(0)
  • 2021-01-12 13:31

    The more statements you put, the less specific about the cause of exception you can be potentially.

    But of course it depends if the function calls/statements carry overlapped exceptions i.e. if all exceptions can be accounted for in a specific manner, then it is still ok.

    In your example, you seem to be having non-overlapped exceptions so your first form is ok.

    0 讨论(0)
  • 2021-01-12 13:32

    Wrap your critical parts to keep your message clear and to the point.

    0 讨论(0)
提交回复
热议问题