php的错误和异常处理

php中文网
发布: 2016-07-29 09:15:14
原创
1259人浏览过

与java不同,在php中,异常必须手动抛出.

抛出并捕获一个异常,示例:

<?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/>";
}
登录后复制
显示结果:

php的错误和<strong>异常处理</strong>
Exception类的内置方法:

getCode()——返回传递给构造函数的代码;

立即学习PHP免费学习笔记(深入)”;

getMessage()——返回传递给狗仔函数的消息;

getFile()——返回产生异常的代码文件的完整路径;

getLine()——返回代码文件中产生异常的代码行号

getTrance——返回一个包含了产生异常的代码回退路径的数组;

getTranceAsString——返回与getTrance()方向相同的消息,该消息将被格式化一个字符串;

__toString()——允许简单地显示一个Exception对象,并且给出以上所有方法可以提供的信息。

自学 PHP、MySQL和Apache
自学 PHP、MySQL和Apache

本书将PHP开发与MySQL应用相结合,分别对PHP和MySQL做了深入浅出的分析,不仅介绍PHP和MySQL的一般概念,而且对PHP和MySQL的Web应用做了较全面的阐述,并包括几个经典且实用的例子。 本书是第4版,经过了全面的更新、重写和扩展,包括PHP5.3最新改进的特性(例如,更好的错误和异常处理),MySQL的存储过程和存储引擎,Ajax技术与Web2.0以及Web应用需要注意的安全

自学 PHP、MySQL和Apache 400
查看详情 自学 PHP、MySQL和Apache

用户自定义异常示例:

<?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;
}
登录后复制

一个应用异常处理的示例:文件I/O处理

首先需要创建一个异常类的文件: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/>";
    }
}
登录后复制
再在主文件processorder.php文件中引入file.Exception.php文件

<strong>require</strong>_once ("file_<strong>Exception</strong>.php");
登录后复制
异常处理关键代码:

 //设置文件输出内容和格式
    $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;
    }
登录后复制

以上就介绍了php的错误和异常处理,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

PHP速学教程(入门到精通)
PHP速学教程(入门到精通)

PHP怎么学习?PHP怎么入门?PHP在哪学?PHP怎么学才快?不用担心,这里为大家提供了PHP速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号