Skip to Content
PHP 5 Kochbuch, Third Edition
book

PHP 5 Kochbuch, Third Edition

by David Sklar, Adam Trachtenberg, Carsten Lucke, Matthias Brusdeylins, Ulrich Speidel, Stephan Schmidt
September 2009
Intermediate to advanced content levelIntermediate to advanced
912 pages
48h 11m
German
O'Reilly Verlag
Content preview from PHP 5 Kochbuch, Third Edition

9.6 PHP-Fehler- und Warnmeldungen in Ausnahmen umwandeln

Problem

Sie wollen, dass bei allen Problemen, die in PHP Fehler- und/oder Warnmeldungen verursachen, stattdessen eine Exception geworfen wird. Zum Beispiel wollen Sie Code absichern, in dem schon andere Exceptions geworfen werden.

Lösung

Deklarieren Sie eine Error-Handler-Funktion, in der Sie eine ErrorException werfen, und registrieren Sie diese Funktion mit set_error_handler():

function exception_error_handler($errno, $errstr, $errfile, $errline ) {
    throw new ErrorException($errstr, 0, $errno, $errfile, $errline);
}
set_error_handler("exception_error_handler", E_ALL - E_NOTICE);


try {
    $file = file_get_contents("/pfad/nach/nirgendwo");
}
catch (Exception $e) {
    echo "File not found!";
}

Diskussion ...

Become an O’Reilly member and get unlimited access to this title plus top books and audiobooks from O’Reilly and nearly 200 top publishers, thousands of courses curated by job role, 150+ live events each month,
and much more.
Start your free trial

You might also like

PHP programmieren unter Windows

PHP programmieren unter Windows

Arno Hollosi

Publisher Resources

ISBN: 9783868993271Purchase book