Skip to content

Instantly share code, notes, and snippets.

@Veve2
Created June 14, 2017 14:41
Show Gist options
  • Save Veve2/f080f5382d98535d5ad27c6961eb71fb to your computer and use it in GitHub Desktop.
Save Veve2/f080f5382d98535d5ad27c6961eb71fb to your computer and use it in GitHub Desktop.
Error to Exception error_handler for PHP
<?php
function exception_error_handler($severity, $message, $file, $line) {
if (!(error_reporting() & $severity)) {
// This error code is not included in error_reporting
return;
}
throw new ErrorException($message, 0, $severity, $file, $line);
}
set_error_handler("exception_error_handler");
@Veve2
Copy link
Author

Veve2 commented Jun 14, 2017

Example of use (after including/inserted at top the code):

<?php
/* Trigger exception */
strpos();
?>

Another use is to catch file_get_content errors (uncatchable) by converting them to exceptions (catchable)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment