復制代碼 代碼如下:
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>";
新聞熱點
疑難解答