With an exceptional choice of exceptions, I always used only the PHP basic Exception class, only with a different message, for example:
<?php if($a < $b){ throw new Exception('a is smaller than b'); }elseif($a > $b){ throw new Exception('a is larger than b'); }
However, I recently noticed that some people throw different classes of exceptions, for example:
<?php if($a < $b){ throw new aIsSmallerThanBException(); }elseif($a > $b){ throw new aIsLargerThanBException(); }
What are the advantages / disadvantages of these two methods? Should I do it in the second style? I noticed that during unit testing with PHPUnit you can say that you expect a specific exception class using setExpectedException (). Does this mean that the second method is better in terms of code verification?
Thanks!
source share