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

前端 未结 14 1433
忘掉有多难
忘掉有多难 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:38

    In general, try-catch blocks are great because they will break (move to the catch statement) whenever the exception occurs. If-else blocks rely on you predicting when the error will happen.

    Edit: Also, catch blocks won't stop your code from halting when an error is hit.

提交回复
热议问题