Throwing an Exception
Voorbeeld: Een uitzondering opwerpen
<?php
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
return 1/$x;
}
try {
echo inverse(5) . "\n";
echo inverse(0) . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
// Voortzetten van de uitvoering
echo "Hello World\n";
?>
0.2
Caught exception: Division by zero.
Hello World
Voorbeeld: Uitzonderingsafhandeling met een finally-blok
<?php
function inverse($x) {
if (!$x) {
throw new Exception('Division by zero.');
}
return 1/$x;
}
try {
echo inverse(5) . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
} finally {
echo "First finally.\n";
}
try {
echo inverse(0) . "\n";
} catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
} finally {
echo "Second finally.\n";
}
// Voortzetten van de uitvoering
echo "Hello World\n";
?>
0.2
First finally.
Caught exception: Division by zero.
Second finally.
Hello World
Voorbeeld: Interactie tussen het finally-blok en return
<?php
function test() {
try {
throw new Exception('foo');
} catch (Exception $e) {
return 'catch';
} finally {
return 'finally';
}
}
echo test();
?>
finally
Voorbeeld: Geneste uitzondering
<?php
class MyException extends Exception { }
class Test {
public function testing() {
try {
try {
throw new MyException('foo!');
} catch (MyException $e) {
// opnieuw opwerpen
throw $e;
}
} catch (Exception $e) {
var_dump($e->getMessage());
}
}
}
$foo = new Test;
$foo->testing();
?>
string(4) "foo!"
Voorbeeld: Multi catch uitzondering afhandeling
<?php
class MyException extends Exception { }
class MyOtherException extends Exception { }
class Test {
public function testing() {
try {
throw new MyException();
} catch (MyException | MyOtherException $e) {
var_dump(get_class($e));
}
}
}
$foo = new Test;
$foo->testing();
?>
string(11) "MyException"
Voorbeeld: Het weglaten van de gevangen variabele
Alleen toegestaan in PHP 8.0.0 en later.
<?php
class SpecificException extends Exception {}
function test() {
throw new SpecificException('Oopsie');
}
try {
test();
} catch (SpecificException) {
print "A SpecificException was thrown, but we don't care about the details.";
}
?>
A SpecificException was thrown, but we don't care about the details.
Voorbeeld: Throw als een expressie
Alleen toegestaan in PHP 8.0.0 en later.
<?php
function test() {
do_something_risky() or throw new Exception('It did not work');
}
function do_something_risky() {
return false; // Simuleer falen
}
try {
test();
} catch (Exception $e) {
print $e->getMessage();
}
?>
It did not work
Voorbeeld: Uitzondering in try en in finally
<?php
try {
try {
throw new Exception(message: 'Third', previous: new Exception('Fourth'));
} finally {
throw new Exception(message: 'First', previous: new Exception('Second'));
}
} catch (Exception $e) {
var_dump(
$e->getMessage(),
$e->getPrevious()->getMessage(),
$e->getPrevious()->getPrevious()->getMessage(),
$e->getPrevious()->getPrevious()->getPrevious()->getMessage(),
);
}
string(5) "First"
string(6) "Second"
string(5) "Third"
string(6) "Fourth"