ExeptionsExeptions are invalid situations that occur during regular processing that prevent the flow from continuing normally until the situation is somehow resolved.
In PHP we are faced with an exception model similar to other programming languages.
throw
Instruction to throw an exception.
Throwable
Throwable is the base interface for any object that can be thrown via a throw statement including Error and Exception.
PHP classes cannot implement the Throwable interface directly, and must instead extend Exception.
<?=
Exception implements Throwable {
/* Properties */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Methods */
public __construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] )
final public getMessage ( void ) : string
final public getPrevious ( void ) : Throwable
final public getCode ( void ) : mixed
final public getFile ( void ) : string
final public getLine ( void ) : int
final public getTrace ( void ) : array
final public getTraceAsString ( void ) : string
public __toString ( void ) : string
final private __clone ( void ) : void
}
?>
<?=
Throwable {
/* Methods */
abstract public getMessage ( void ) : string
abstract public getCode ( void ) : int
abstract public getFile ( void ) : string
abstract public getLine ( void ) : int
abstract public getTrace ( void ) : array
abstract public getTraceAsString ( void ) : string
abstract public getPrevious ( void ) : Throwable
abstract public __toString ( void ) : string
}
?>
<?=
Error implements Throwable {
/* Properties */
protected string $message ;
protected int $code ;
protected string $file ;
protected int $line ;
/* Methods */
public __construct ([ string $message = "" [, int $code = 0 [, Throwable $previous = NULL ]]] )
final public getMessage ( void ) : string
final public getPrevious ( void ) : Throwable
final public getCode ( void ) : mixed
final public getFile ( void ) : string
final public getLine ( void ) : int
final public getTrace ( void ) : array
final public getTraceAsString ( void ) : string
public __toString ( void ) : string
final private __clone ( void ) : void
}
?>
catch
Exception catch instruction.
finally
In PHP 5.5 and later, a finally block may also be specified after or instead of catch blocks.
try
Code may be surrounded in a try block where the code can be placed to facilitate the catch of potential exceptions.
Each try must have at least one corresponding catch or finally block.
EXERCISE
<?php
function inverse($x) {
if (!$x) {
throw new Exception('<br>Division by zero.');
}
return "1/$x = ". 1/$x;
}
$v1a = 5;
$v1b = 0;
try {
echo inverse($v1a) . "<br>";
echo inverse($v1b) . "<br>";
} catch (Exception $e) {
echo '<br>Caught exception: ', $e->getMessage(), "<br><br>";
echo 'Previous: ' . $e->getPrevious(), "<br><br>Code:<br>";
print_r ($e->getCode());
echo "<br><br>";
echo 'File:<br>' . $e->getFile(), "<br><br>";
echo 'Line:<br>' . $e->getLine(), "<br><br>Trace Array:<br>";
var_dump($e->getTrace());
echo "<br><br>";
echo 'Trace AsString:<br>' . $e->getTraceAsString();
echo "<br><br>";
}
// Continue execution
echo "For now, that's all!<br>";
?>
EXERCISE
<?php
/*
* To run this exercise simultaneously with the first,
* you will need to change the function name.
* If this is not done, the flow will be
* interrupted by a Fatal error.
*/
function inverse($x) {
if (!$x) {
throw new Exception('<br>Division by zero.');
}
return "1/$x = ". 1/$x;
}
$v1a = 5;
$v1b = 3;
try {
echo inverse($v1a) . "<br>";
echo inverse($v1b) . "<br>";
} catch (Exception $e) {
echo '<br>Caught exception: ', $e->getMessage(), "<br><br>";
echo 'Previous: ' . $e->getPrevious(), "<br><br>Code:<br>";
print_r ($e->getCode());
echo "<br><br>";
echo 'File:<br>' . $e->getFile(), "<br><br>";
echo 'Line:<br>' . $e->getLine(), "<br><br>Trace Array:<br>";
var_dump($e->getTrace());
echo "<br><br>";
echo 'Trace AsString:<br>' . $e->getTraceAsString();
echo "<br><br>";
}
// Continue execution
echo "For now, that's all!<br>";
?>