NOVINKA: Najžiadanejšie rekvalifikačné kurzy teraz s 50% zľavou + kurz AI ZADARMO. Nečakaj, táto ponuka dlho nevydrží! Zisti viac:

PHP - set_exception_handler

Trieda

Koreň \ Bez triedy

Metóda - set_exception_handler

(PHP 5, PHP 7)

The function is used to define a user exception handler, meaning we can catch exceptions on the highest level when they'd cause the termination of the script otherwise. This function captures all the exceptions which are not captured in try-catch blocks. The execution of the script will stop after the function is called!

Note: The exception handler captures Exceptions only, not errors. For capturing errors, use the set_error_handler() function!

Procedurálne

Parametre

NázovDátový typPredvolená hodnotaPopis
$exception_handlercallable

The name of a function to be used as the exception handler.

Mávratovej hodnoty

Vracia: callable

Returns the name of the previously declared exception handler, or null on failure or if no previous exception handler was declared.

Príklady

Without try-catch:

<?php

function myHandler($exception)
{
    echo '-----------' . "\n";
    echo 'We got an Exception!' . "\n";
    echo 'It says: ' . $exception->getMessage() . "\n";
    echo 'It is in the file ' . $exception->getFile() . ':' . $exception->getLine() . "\n";
    echo '-----------' . "\n";
}

set_exception_handler('myHandler');

echo 'Hello World!' . "\n";
throw new Exception('Test exception');
echo 'Goodbye World!';

And the same with try-catch:

<?php

function myHandler($exception)
{
    echo '-----------' . "\n";
    echo 'We got an Exception!' . "\n";
    echo 'It says: ' . $exception->getMessage() . "\n";
    echo 'And it is in file ' . $exception->getFile() . ':' . $exception->getLine() . "\n";
    echo '-----------' . "\n";
}

set_exception_handler('myHandler');

echo 'Hello World!' . "\n";
try {
    throw new Exception('Test exception');
}
catch (Exception $ex)
{
    echo 'Exception! But we are in a try-catch, so the script will continue.' . "\n";
}
echo 'Goodbye World!';

Súvisiace manuály

        Aktivity