Exceptions
Multiple Catch
Separate catch blocks can handle different exception classes.
Multiple Catch
multiple_catch.php
<?php
$mode = ;
try {
if ($mode === "empty") {
throw new InvalidArgumentException("missing");
}
if ($mode === "short") {
throw new LengthException("short");
}
$status = "accepted";
} catch (InvalidArgumentException $error) {
$status = "missing";
} catch (LengthException $error) {
$status = "too_short";
}
echo "mode=" . $mode . "\n";
echo "status=" . $status . "\n";
<?php
$mode = ;
try {
if ($mode === "empty") {
throw new InvalidArgumentException("missing");
}
if ($mode === "short") {
throw new LengthException("short");
}
$status = "accepted";
} catch (InvalidArgumentException $error) {
$status = "missing";
} catch (LengthException $error) {
$status = "too_short";
}
echo "mode=" . $mode . "\n";
echo "status=" . $status . "\n";
<?php
$mode = ;
try {
if ($mode === "empty") {
throw new InvalidArgumentException("missing");
}
if ($mode === "short") {
throw new LengthException("short");
}
$status = "accepted";
} catch (InvalidArgumentException $error) {
$status = "missing";
} catch (LengthException $error) {
$status = "too_short";
}
echo "mode=" . $mode . "\n";
echo "status=" . $status . "\n";
specific handler
Specific exception classes let code name the kind of recovery it is doing.