38

I would like to be able to throw a fatal, uncatchable error in my php class when a user of my class abuses it for something I did not intend. I don't want him/her to be able to recover with a catch clause. I know about trigger_error, but I can only make it issue warnings or notices.

5
  • 1
    Why bother? Whoever is implementing your class: 1. Is a programmer. 2. Has access to the source. 3. Can and likely will delete the trigger_error() call. Commented Apr 8, 2013 at 19:18
  • 2
    @Sammitch: I want to make my intentions clear. I want to let whoever uses my code know that what they are doing is not how I intended them to use it. To me throwing an exception sends the message "you deal with this undefined behaviour." I want to send the message: "Don't do it that way, do it this way instead". Commented Apr 8, 2013 at 19:26
  • 2
    Example libraries. Where you should for example absolutely NOT do something if some condition is not true. (Useful for people who don't like reading documentations :P) Commented Apr 8, 2013 at 19:27
  • @bwoebi: Exactly my situation ;-) Commented Apr 8, 2013 at 19:29
  • I realize this is a 7 year old question, but even so, someone might come upon it. The idea you had that an exception sends the message to the programmer to deal with undefined behavior is totally wrong. That is not what exceptions mean. The only way the programmer would think that is if they don't know anything about programming or they make random and wild assumptions. Exceptions are just cleaner. Also triggering an error doesn't mean they can't recover from it. They could still use error handling in a similar way to exception handling. Commented May 30, 2020 at 4:28

5 Answers 5

62

E_USER_ERROR is the suited constant.

trigger_error("Fatal error", E_USER_ERROR);

See also the first example of the manual page and the list of PHP Errors (only ones beginning with E_USER* may be issued from trigger_error).

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

1 Comment

E_USER_ERROR Warning Usage of this constant with trigger_error() is deprecated as of PHP 8.4.0. It is recommended to either throw an Exception or call exit() instead. -- PHP Manual
14

Note: if you're using a custom error handler (see set_error_handler) E_USER_ERROR will NOT halt/exit/die unless the error handler returns false

nutshell : your custom error handler effectively determines if E_USER_ERROR is treated as a fatal

2 Comments

Very true. As I've noted in another place, you can create a fatal error via: if($isTestMode==1){ eval("noSuchFunction();"); } This method will always work.
Thanks Brad this really helped me, I am using custom error handler function. Although it was 'handling the error' I triggered with the flag E_USER_ERROR it was not causing a 'fatal' error until I returned false from my error handler function.
7
debug_print_backtrace();
trigger_error("As much information as you can provide, please", E_USER_ERROR);
exit();

exit() terminates the PHP script entirely. The more information you can provide users or developers about the error, the better. Error codes are passé.

The use of E_USER_ERROR should terminate the script anyway, so moved debug_print_backtrace() before trigger_error().

1 Comment

absolutely, adding die; or exit; command after throwing such error is only way to be almost 100% sure. Only way to escape from it (expect php.ini disabled functions) is runkit_function_remove
2

If you are using PHP 7 or higher, Error class works too:

$flag = false;

try {
    if ($flag == false) {
        throw new Error('An error occured');
    }
} catch (Error $e) {
    echo $e->getMessage();
}

If you put throw new Error('An error occured'); outside the Try Catch, You will get a Fatal Error.

1 Comment

I don't think this is correct. Throwing an error halts script execution but throws it up to the calling class/function to decided what to do with it. So it depends on what the code that executed before this wants to do with it. This lets you do things like try/catch in the parent and throw errors up to it in the children.
0

Just trying to break our acceptance env. and stumbled upon a nice and simple way to induce a fatal error.

Simply call non existent function. As recommended here

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.