exceptions php code example
Example 1: php catch exception
<?php
function inverse($x) {
if (!$x) {
throw new Exception('Division durch Null.');
}
return 1/$x;
}
try {
echo inverse(5) . "\n";
echo inverse(0) . "\n";
} catch (Exception $e) {
echo 'Exception abgefangen: ', $e->getMessage(), "\n";
}
echo "Hallo Welt\n";
?>
Example 2: exceptions php
<?php
function inverse($x) {
if (!$x) {
throw new Exception('Division durch Null.');
}
return 1/$x;
}
try {
echo inverse(5) . "\n";
echo inverse(0) . "\n";
} catch (Exception $e) {
echo 'Exception abgefangen: ', $e->getMessage(), "\n";
}
echo "Hallo Welt\n";
?>
Example 3: php multiple catch exceptions
try
{
}
catch(AError | BError $e)
{
}
catch(Exception $e)
{
}