10

While answering PHP rename() doesn't throws exception on error I was wondering if there are any native PHP functions which throw a built-in Exception, beside the SPL stuff?

Community
  • 1
  • 1
powtac
  • 40,542
  • 28
  • 115
  • 170

4 Answers4

6

PDO can be configured to throw exceptions

dev-null-dweller
  • 29,274
  • 3
  • 65
  • 85
  • 1
    Ahh yes, but does that count, since it's OO anyway...? I interpret this as referring to procedural style functions only (although I could be wrong) – DaveRandom May 15 '12 at 19:35
  • Interesting! But as far as I can see it's only in a OO/Class context not when calling a simple function. Or? – powtac May 15 '12 at 19:36
  • 1
    I doubt there is any without OO context, just quick greping inside php source folder for `zend_throw_exception` shows that there are only in OO context. – dev-null-dweller May 15 '12 at 20:58
5

Not really. If you read the note on that page you linked:

Note:

Internal PHP functions mainly use Error reporting, only modern Object oriented extensions use exceptions. However, errors can be simply translated to exceptions with ErrorException.

animuson
  • 53,861
  • 28
  • 137
  • 147
  • Thanks! That is reasonable! But it says "*mainly* use Error reporting". Is there is really none? – powtac May 15 '12 at 19:37
  • 2
    @powtac: Exceptions weren't added to PHP until PHP5. So anyone who has been a long-term user of any form of PHP will naturally expect the normal PHP functions to return an error, and not an Exception. So randomly starting to throw in functions that start returning Exceptions instead of errors would throw a lot of people off. Then you'd need two handling methods as well: one for normal errors and one for Exceptions. Why mix the two together? – animuson May 15 '12 at 19:43
1

PHP 5 has an exception model similar to that of other programming languages.

ErrorException

Moyed Ansari
  • 8,436
  • 2
  • 36
  • 57
1

In addition to PDO (which can be configured to throw exceptions), DateTime and DateTimeZone will both throw exceptions when the constructor is called with invalid data.

Division by zero, though not a native function, will trigger an error, and throw an exception (DivisionByZeroError when using arithmetic operator / since PHP 8), though unless you've changed the error handler with set_error_handler(), the error will stop execution before the exception is thrown.

Community
  • 1
  • 1
Bob Ray
  • 1,105
  • 10
  • 20