与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/>";
}
Exception类的内置方法:
getCode()——返回传递给构造函数的代码;
立即学习“PHP免费学习笔记(深入)”;
getMessage()——返回传递给狗仔函数的消息;
getFile()——返回产生异常的代码文件的完整路径;
getLine()——返回代码文件中产生异常的代码行号;
getTrance——返回一个包含了产生异常的代码回退路径的数组;
getTranceAsString——返回与getTrance()方向相同的消息,该消息将被格式化一个字符串;
__toString()——允许简单地显示一个Exception对象,并且给出以上所有方法可以提供的信息。
本书将PHP开发与MySQL应用相结合,分别对PHP和MySQL做了深入浅出的分析,不仅介绍PHP和MySQL的一般概念,而且对PHP和MySQL的Web应用做了较全面的阐述,并包括几个经典且实用的例子。 本书是第4版,经过了全面的更新、重写和扩展,包括PHP5.3最新改进的特性(例如,更好的错误和异常处理),MySQL的存储过程和存储引擎,Ajax技术与Web2.0以及Web应用需要注意的安全
400
用户自定义异常示例:
<?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;
}首先需要创建一个异常类的文件: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/>";
}
}<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速学教程(入门到精通),有需要的小伙伴保存下载就能学习啦!
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号