异常处理

来源:互联网 发布:图像空间的消隐算法 编辑:程序博客网 时间:2024/06/16 16:42
<meta http-equiv="Content-Type" content="text/html;charset=utf-8"/>
<?php
error_reporting(E_ALL);
class text{
    protected $conn=null;
    public function __construct(){
        $this->conn=mysql_connect('localhost','root','r1oot');//这里故意写错:)
        if (!$this->conn) {
            $e=new Exception('错误',001);1,new Exception('错误原因','错误代码')      2.Exception是所有异常的基类。
            throw $e;
        }
    }
}
try {
    $a=new text();

}

catch (Exception $e) {

    echo $e->getMessage(),'<br/>',
    $e->getCode(),'<br/>',
    $e->getFile(),'<br/>',
    $e->getLine(),'<br/>';
}

print_r($a);
if ($a instanceof text) {
    echo '对象创建成功,连接成功';
}

else{echo '对象创建失败,连接失败';}


抛出异常后,对象未创建成功

0 0