Исключения в PHP

Краткое содержание лекции

Теоретические задания | Практические задания

<?php
Exception
{
/* Properties */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Methods */
public __construct ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]] )
final public string getMessage ( void )
final public Exception getPrevious ( void )
final public mixed getCode ( void )
final public string getFile ( void )
final public int getLine ( void )
final public array getTrace ( void )
final public string getTraceAsString ( void )
public string __toString ( void )
final private void __clone ( void )
}
?>
<?php
try {
    throw new \Exception("Error Processing Request", 1);
} catch (\Exception $e) {
    throw new \Exception("Error Processing Request", 1, $e);
}
?>
<?php
//php 5.5
try {
    throw new \Exception("Error Processing Request", 1);
} catch (\Exception $e) {
    throw new \Exception("Error Processing Request", 1, $e);
} finally {
    echo 'You see this text always.';
}
?>

Собственное исключение

<?php
class MyException extends Exception
{
}
?>
<?php
try {
    if ($result == false) {
        throw new MyException('Throw MyException');
    }
} catch (MyException $e) {
    echo $e->getMessage() . \PHP_EOL;
    echo $e->getTraceAsString();
}
?>
<?php
try {
    if ($result == false) {
        throw new MyException('Throw MyException');
    } else {
        throw new \Exception('Throw Exception');
    }
} catch (MyException $e) {
    echo $e->getMessage() . \PHP_EOL;
    echo $e->getTraceAsString();
} catch (\Exception $e) {
     echo $e->getMessage() . \PHP_EOL;
     echo $e->getTraceAsString();
 }
?>

SPL