I throw new Exception when I can catch a failure and do something about it. If I can't do something about it then I just trigger_error().
Now there's something new to me in PHP 7: throw new Error.
E.g.,
if (!mail(...))
throw new Error('...');
or
if (!mail(...))
trigger_error('...');
If I don't want to catch the error or do something in case mail() fails should I use throw new Error() or just the plain old trigger_error()?
What instances should we use throw new Error() vs a simple trigger_error()?
The \Error class was, as you already mentioned, introduced in PHP 7.
It acts exactly like an exception since it implements Throwable.
It's also subclasses by the following:
ArithmeticError
DivisionByZeroError
AssertionError
ParseError
TypeError
ArgumentCountError
However, it does not behave like trigger_error(), in fact it behaves exactly like an exception(mostly because it is one).
If you trigger_error() you can't catch it, because it's not an exception. Although there are workarounds.
This is mostly guestimating on my part.
I assume this exists for a more clear separation of Errors and Exceptions, possibly future plans include deprecating trigger_error and leaving just Error.
To answer your question.
I'd stick to throwing stuff rather than trigger_error. It's catchable, it's OOP.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With