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.
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
).