复制代码 代码如下:
if(){
}else{
}
try {
}catch(异常对象){
}
复制代码 代码如下:
class OpenFileException extends Exception {
function __construct($message = null, $code = 0){
parent::__construct($message, $code);
echo "wwwwwwwwwwwwwww<br>";
}
function open(){
touch("tmp.txt");
$file=fopen("tmp.txt", "r");
return $file;
}
}
class DemoException extends Exception {
function pro(){
echo "处理demo发生的异常<br>";
}
}
class TestException extends Exception {
function pro(){
echo "这里处理test发生的异常<br>";
}
}
class HelloException extends Exception {
}
class MyClass {
function openfile(){
$file=@fopen("tmp.txt", "r");
if(!$file)
throw new OpenFileException("文件打开失败");
}
function demo($num=0){
if($num==1)
throw new DemoException("演示出异常");
}
function test($num=0){
if($num==1)
throw new TestException("测试出错");
}
function fun($num=0){
if($num==1)
throw new HelloException("###########");
}
}
try{
echo "11111111111111<br>";
$my=new MyClass();
$my->openfile();
$my->demo(0);
$my->test(0);
$my->fun(1);
echo "22222222222222222<br>";
}catch(OpenFileException $e){ //$e =new Exception();
echo $e->getMessage()."<br>";
$file=$e->open();
}catch(DemoException $e){
echo $e->getMessage()."<br>";
$e->pro();
}catch(TestException $e){
echo $e->getMessage()."<br>";
$e->pro();
}catch(Exception $e){
echo $e->getMessage()."<br>";
}
var_dump($file);
echo "444444444444444444444<br>";
新闻热点
疑难解答