class a{
function __construct(){
echo "start to construct a obj\n";
}
function divide($a,$b)
{
//exit(1);//__destruct called
return $a/$b; //__destruct called when $b=0;
}
function add($a,$b)
{
throw new Exception ("daone");//__destruct called
return $a+$b;
}
function __destruct(){
echo "start to destruct\n";
}
}
try{
$first = new a();
//$r = $first->divide(4,0);
$first->add(4,0);
}
catch(Exception $e)
{
echo "cath exception \n";
function __construct(){
echo "start to construct a obj\n";
}
function divide($a,$b)
{
//exit(1);//__destruct called
return $a/$b; //__destruct called when $b=0;
}
function add($a,$b)
{
throw new Exception ("daone");//__destruct called
return $a+$b;
}
function __destruct(){
echo "start to destruct\n";
}
}
try{
$first = new a();
//$r = $first->divide(4,0);
$first->add(4,0);
}
catch(Exception $e)
{
echo "cath exception \n";
}
从上面的例子可以看出,php抛出异常(不管外面有没有try catch),直接退出的情况下,都会调用析构函数。