MyException.php
<?php
class MyException extends Exception
{
public function __construct($message, $code = 0)
{
parent::__construct($message, $code);
}
public function __toString()
{
$message = "<h2>出现异常了,信息如下</h2>";
$message .= "<p>" . __CLASS__ . "[{$this->code}]:{$this->message}</p>";
return $message;
}
public function test()
{
echo "this is a test";
}
public function stop()
{
exit('script end...');
}
//自定义其他方法
}
try {
echo "出现异常啦";
throw new MyException('测试自定义异常', 3);
} catch (MyException $e) {
echo $e->getMessage();//测试自定义异常
echo "<hr/>";
echo $e;//tostring
echo "<hr/>";
$e->test();//this is a test
echo "<hr/>";
$e->stop();//script end...
}
echo "<hr/>";
echo "continue...";
?>