What is the advantage of using try {} catch {} versus if {} else {}

前端 未结 14 1378
忘掉有多难
忘掉有多难 2020-11-29 18:19

I am switching from plain mysql in php to PDO and I have noticed that the common way to test for errors is using a try / catch combination instead of if / else combinations.

相关标签:
14条回答
  • 2020-11-29 18:45

    Everybody else had good answers - but I figured I would throw my own in:

    1. Try/Catch is an actual exception handling mechanism - so if you change your exceptions, it will automatically work on all try/catch statements.
    2. Try/Catch gives the opportunity to run code even in the case of a major exception that might kill the if/else and in addition, the try statement can be rolled back (if you're savvy).
    0 讨论(0)
  • 2020-11-29 18:47

    In php by using Try Catch with inheritence, We can throw exception from another class.

    Example :- I am in the controller and validating user data by using Models.

    If any error triggers, I just have to throw exception from Model methods.

    The execution in try will break and catched in the Catch Block.

    So There is less overhead of returning bool vales and checking that.

    Apart from this Try Catch works great When using in chain ( Try - Catch inside another Try - Catch ).

    0 讨论(0)
  • 2020-11-29 18:48

    Try/Catch totally separates the error handling logic from the object business logic.

    0 讨论(0)
  • 2020-11-29 18:50

    @Perchik:

    My general philosophy of error handling:

    You should use if / else to handle all cases you expect. You should not use try {} catch {} to handle everything (in most cases) because a useful Exception could be raised and you can learn about the presence of a bug from it. You should use try {} catch {} in situations where you suspect something can/will go wrong and you don't want it to bring down the whole system, like network timeout/file system access problems, files doesn't exist, etc.

    Vexing exceptions

    0 讨论(0)
  • 2020-11-29 18:52

    This question has been asked more than a decade ago out of the wrong premise. In reality if and try are incomparable matters. Sadly, but up to this day people catastrophically confuse exceptions with try catch, thinking one is inseparable from another.

    In the way it is asked, indeed it makes very little sense to change if {} to try {} in the meaning of obligatory try wrapping a single line of code to test for the error.

    However the actual question is What is the advantage of using exceptions versus if {} else {}.

    And it starts to make a whole world of sense immediately: exceptions allow automated error reporting, when neither try catch nor if else is ever have to be written.

    An exception is essentially an automated way to write if ($result == FALSE) raise_error(); Without exceptions you are bound to test every operation's result manually. It would be just stupid to recreate the same behavior using exceptions. In most cases a thrown exception should be left alone, unless there is a certain handling scenario. In all other cases it has to bubble up elsewhere, hence no try {} catch {} has to be written.

    0 讨论(0)
  • 2020-11-29 18:53

    I'd use the try/catch block when the normal path through the code should proceed without error unless there are truly some exceptional conditions -- like the server being down, your credentials being expired or incorrect. I wouldn't necessarily use it to handle non-exceptional errors -- say like the current user not being in the correct role. That is, when you can reasonably expect and handle an error that is not an exceptional condition, I think you should do your checks.

    In the case that you've described -- setting up and performing a query, a try/catch block is an excellent way to handle it as you normally expect the query to succeed. On the other hand, you'll probably want to check that the contents of result are what you expect with control flow logic rather than just attempting to use data that may not be valid for your purpose.

    One thing that you want to look out for is sloppy use of try/catch. Try/catch shouldn't be used to protect yourself from bad programming -- the "I don't know what will happen if I do this so I'm going to wrap it in a try/catch and hope for the best" kind of programming. Typically you'll want to restrict the kinds of exceptions you catch to those that are not related to the code itself (server down, bad credentials, etc.) so that you can find and fix errors that are code related (null pointers, etc.).

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