20,359
社区成员
发帖
与我相关
我的任务
分享
<?php
class MyException extends Exception { }
class Test {
public function testing() {
try {
try {
throw new MyException('foo!');
} catch (MyException $e) {
// rethrow it
throw $e;
}
} catch (Exception $e) {
var_dump($e->getMessage());
}
}
}
$foo = new Test;
$foo->testing();
?>
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";
}
// Continue execution
echo "Hello World\n";
以上例程会输出:
0.2
Caught exception: Division by zero.
Hello World
try {
throw new Exception($error);
} catch (Exception $e) {
echo $e->getMessage();
}
set_exception_handler('myException');
test(-1);
function test($a){
if($a < 0){
throw new Exception('error');
}
return $a;
}
function myException($e){
$msg='code : '.$e->getCode().'<br>message : '.$e->getMessage();
echo $msg;
}