This program throws different exceptions based on input and catches them in separate catch blocks, printing a message for each.
<?php
function testException($type) {
try {
if ($type === 'invalid') {
throw new InvalidArgumentException("Invalid argument provided.");
} elseif ($type === 'runtime') {
throw new RuntimeException("Runtime problem occurred.");
} else {
throw new Exception("General error.");
}
} catch (InvalidArgumentException $e) {
echo "Caught InvalidArgumentException: " . $e->getMessage() . "\n";
} catch (RuntimeException $e) {
echo "Caught RuntimeException: " . $e->getMessage() . "\n";
} catch (Exception $e) {
echo "Caught Exception: " . $e->getMessage() . "\n";
}
}
testException('invalid');
testException('runtime');
testException('other');
?>