php5异常处理学习,菜鸟的体会
时间:2008-07-02
来源:互联网
先了解下什么是异常,php5中引入了对错误处理的一个机制,这个机制的作用就是帮助将错误处理代码和你的使用应用要完成的具体工作部分分离出来,这个就称为异常。
异常的工作原理:
try
{
代码处理程序;
if(代码处理发生错误)throw new Exception('抛出一个异常');//使用throw关键字,后面是Exception的一个对象
//需要说明的是php5异常不会自动抛出异常
//抛出异常后下面处理程序不再执行
代码处理程序;
}
catch Exception $e
{
处理异常;
//如:echo 'Exception '.$e->getCode().':'.$e->getMessage().'in'.$e->getFile().'on line'.$e->getLine();
}
看看抛出的异常类系统是如何定义的
class Exception
{
protected $message='Unknown exception';
protected $code=0;
protected $file;
protected $line;
function __construct($message=null,$code=0);
final function getMessage();
final function getCode();
final function getFile();
final function getLine();
final function getTrace();
final function getTraceAsString();//与getTrace()一样,只不过它将格式化为字符串
function __toString();//需要对象的字符串表示时会自动调用这个方法,也就是一旦有echo或print直接输出Exception实例时就会被调用
}
Exception的属性不能在调用代码中直接访问,而必须使用获取方法获得其属性值,只用$message,$code能有用户抛出异常时设置,即给Exception类的构造函数完成。由Exception类可以看出我们完全可以继承Exception类创建我们自己的异常类,在检测错误时不再抛出系统默认的异常对象,而是我们自定义的异常类对象,但我们只能继承构造函数、toString()方法和Exception的属性。
class myException extends Exception
{
function __toString()
{
return '<div class="error">Exception '.$this->getCode().':'.$this->getMessage().'in'.$this->getFile().'on line'.$this->getLine().'</div>';//改写抛出异常结果
}
}
在代码中调用我们自定义的异常类
try
{
throw new myException('错误',10);//为了简单,直接抛出自定义异常对象
}
catch(myException $e)
{
echo $e;//由于我们的自定义异常已经改变了输出方式,所以这里直接输入异常对象即可,原因为一旦有echo或print直接输出Exception实例时就会被调用__toString()
}
以上单间的介绍了异常,下面我们把异常引入我们的数据库处理类中看看如何使用
<?php
class myException extends Exception{
function __construct($message=null,$code=0)
{
parent::__construct($message,$code);
}
function __toString()
{
return '<div class="error">Exception '.$this->getCode().':'.$this->getMessage().'in File:'.$this->getFile().' on line:'.$this->getLine().'</div>';//改写抛出异常结果
}
}
class mydb {
private $user;//用户名
private $pass;//密码
private $host;//数据库ip地址或localhost
private $db; //数据库名
//构造函数
public function __construct (){
$num_args = func_num_args();
if($num_args > 0){
$args = func_get_args();
$this->host = $args[0];
$this->user = $args[1];
$this->pass = $args[2];
//传入参数后自动调用数据库连接
$this->connect();
}
}
//数据库连接
private function connect (){
//异常处理
try {
if (!$this->db =@ mysql_connect ($this->host,$this->user,$this->pass)){
$exceptionstring = "连接失败:主机、用户名或密码错误 ";
throw new myException ($exceptionstring,0);//抛出自定义异常对象,实例对象时参数错误
}
} catch (myException $e) {
echo $e;
}
}
//数据库选择
public function selectdb ($thedb){
try {
if ([email=!@mysql_select_db]!@mysql_select_db[/email] ($thedb, $this->db)){
$exceptionstring = "数据库: <font color=red>$thedb</font>不存在 ";
throw new myException ($exceptionstring,1);
}
} catch (myException $e) {
echo $e;
}
}
//执行一个update,delete
public function execute ($thequery){
try {
if ([email=!@mysql_query]!@mysql_query[/email] ($thequery, $this->db)){
$exceptionstring = "错误的sql语句: <font color=red>$thequery</font> ";
throw new myException ($exceptionstring,2);
} else {
echo "update/delete受影响: " . mysql_affected_rows () . " 行<br />";
}
} catch (myException $e) {
echo $e;
}
}
//返回插叙结果
public function getrows ($thequery){
try {
if (!$aquery =@ mysql_query ($thequery)){
$exceptionstring = "错误的查询语句: <font color=red>$thequery</font> ";
throw new myException ($exceptionstring,3);
} else {
while ($adata = mysql_fetch_array ($aquery)){
$returnarr[] =$adata;
}
return $returnarr;
}
} catch (myException $e) {
echo $e;
}
}
//析构函数关闭连接
public function __destruct() {
try {
if ([email=!@mysql_close]!@mysql_close[/email] ($this->db)){
$exceptionstring = "关闭连接失败 ";
throw new myException ($exceptionstring,4);
}
} catch (myException $e) {
echo $e;
}
}
}
//实例类
$mydb = new mydb ("localhost","root","123456");
//选择数据库
$mydb->selectdb ("tonlong");
//执行更新操作
$adata = $mydb->execute ("update db_news set hits=hits+1 where id=3");
//查询输出
$data = $mydb->getrows ("select title from db_news");
for ($i = 0; $i < count ($data); $i++){
echo $data[$i][0] ."<br />";
}
?>
代码下载
假设密码错误
恳请给出意见
[ 本帖最后由 omcmc 于 2008-7-7 01:07 编辑 ]
作者: omcmc 发布时间: 2008-07-02


作者: benben3221987 发布时间: 2008-07-02
作者: 疯狂小猫 发布时间: 2008-07-02
作者: jingyun 发布时间: 2008-07-03
作者: d9tx 发布时间: 2008-07-03


作者: quanhaier 发布时间: 2008-07-10
外语人才 翻译人才
作者: tuvw242 发布时间: 2008-07-12
作者: slfyeye 发布时间: 2008-08-21
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28