Catching Exceptions in PHP 5
<?php
class MyException extends Exception {
}
class AnotherException extends Exception {
}
class ThrowExample {
public function makeMyException() {
throw new MyException();
}
public function makeAnotherException() {
throw new AnotherException();
}
public function makeError() {
throw new Exception();
}
}
$inst = new ThrowExample();
try {
$inst->makeMyException();
} catch(MyException $e) {
echo "Caught 'MyException'\n";
}
try {
$inst->makeAnotherException();
} catch(MyException $e) {
echo "Caught 'MyException'\n";
} catch(AnotherException $e) {
echo "Caught 'AnotherException'\n";
}
try {
$inst->makeError();
} catch(MyException $e) {
echo "Caught 'MyException'\n";
} catch(AnotherException $e) {
echo "Caught 'AnotherException'\n";
}
?>
Related examples in the same category