php error and exception handling

WBOY
Release: 2016-07-29 09:15:14
Original
973 people have browsed it

Unlike java, in php, exceptions must be thrown manually.

throw and catch an exception, example:

<?php
try{
    throw new <strong>Exception</strong>("A terrible error has occurred",42);
}catch (<strong>Exception</strong> $e){
    echo "<strong>Exception</strong> ".$e->getCode().":".$e->getMessage()."<br/>"."in".$e->getFile()." on line".$e->getLine()."<br/>";
}
Copy after login
shows the result:

php的错误和<strong>Exception handling</strong>
ExceptionBuilt-in methods of the class:

getCode() - Returns the code passed to the constructor;

getMessage() - Returns the pass Message to the paparazzi function;

getFile() - returns the full path of the code file that generated the exception;

getLine() - returns the code line number in the code file that generated the exception ;

getTrance - Returns an array containing the code rollback path that generated the exception;

getTranceAsString - Returns a message in the same direction as getTrance(), which will be formatted String;

__toString() - Allows to simply display an Exception object and give the information that all the above methods can provide.

User-defined exception example:

<?php
//自定义异常
class my<strong>Exception</strong> extends <strong>Exception</strong>{
    function __toString(){
        return "<strong>Exception</strong> ".$this->getCode().":".$this->getMessage()."<br/>"."in".$this->getFile()." on line".$this->getLine()."<br/>";
    }
}

try{
    throw new my<strong>Exception</strong>("A terrible error has occurred",42);
}catch (my<strong>Exception</strong> $m){
    echo $m;
}
Copy after login

An example of applying Exception handling: file I/O processing

First you need to create an exception class file: file_Exception.php

<?php

//自定义文件打开异常
class fileOpen<strong>Exception</strong> extends <strong>Exception</strong>{
    function __toString(){
        return "fileOpen<strong>Exception</strong> ".$this->getCode().":".$this->getMessage()."<br/>"."in".$this->getFile()." on line".$this->getLine()."<br/>";
    }
}

//自定义无法写入异常
class fileWrite<strong>Exception</strong> extends <strong>Exception</strong>{
    function __toString(){
        return "fileWrite<strong>Exception</strong> ".$this->getCode().":".$this->getMessage()."<br/>"."in".$this->getFile()." on line".$this->getLine()."<br/>";
    }
}
//自定义无法获得写锁异常
class fileLock<strong>Exception</strong> extends <strong>Exception</strong>{
    function __toString(){
        return "fileLock<strong>Exception</strong> ".$this->getCode().":".$this->getMessage()."<br/>"."in".$this->getFile()." on line".$this->getLine()."<br/>";
    }
}
Copy after login
and then introduce the file.Exception.php file into the main file processorder.php file

<strong>require</strong>_once ("file_<strong>Exception</strong>.php");
Copy after login
Exception handlingKey code:

 //设置文件输出内容和格式
    $out_put_string=$date."\t".$cloths."件男装\t".$shoes."双鞋子\t".$glasses."副眼镜\t\总价:¥".$total_amount." 收货地址:\t".$address."\n";

    //<strong>异常处理</strong>
    try{
        //打开文件,(追加模式+二进制模式)
        if(!($fp=@fopen("$DOCUMENT_ROOT/L02/files/orders.txt",'ab')))
            throw new fileOpen<strong>Exception</strong>();
        //写操作锁定
        if(!flock($fp,LOCK_EX))
            throw new fileLock<strong>Exception</strong>();
        //将数据写入到文件
        if(!fwrite($fp,$out_put_string,strlen($out_put_string)))
            throw new fileWrite<strong>Exception</strong>();
        //释放已有的锁定
        flock($fp,LOCK_UN);
        //关闭文件流
        fclose($fp);
        echo "<p>数据保存完成</p>";
    }catch (fileOpen<strong>Exception</strong> $foe){
        echo "<p><strong>文件打开失败,请查看服务器是否异常!</strong></p>";
        exit;
    }catch(<strong>Exception</strong> $e){
        echo "<p><strong>您的订单没有提交完成,请再试一次。</strong></p>";
        exit;
    }
Copy after login

The above introduces the error and exception handling of PHP, including the relevant content. I hope it will be helpful to friends who are interested in PHP tutorials.

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template