3

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()?

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.