PHP->异常处理

本文介绍了PHP中的异常处理机制,特别是内置的Exception类的使用方法。包括如何创建异常、捕获异常以及获取异常的相关信息,如错误代码、错误消息、发生异常的文件路径及行号。

1、Exception类

PHP的异常处理中提供了内置类—Exception,其构造函数需要两个参数,一个错误消息和一个错误代码。

getCode() —返回传递给构造函数的代码
getMessage() — 返回传递给构造函数的消息
getFile() —返回产生异常的代码文件的完整路径
getLine() —返回代码文件中产生异常的代码行号
getTrace() —返回一个包含了产生异常的代码回退路径的数组

<?php

try{
    throw new Exception('A terrible error has occurred',42);
}

catch (Exception $e){
    echo "Exception".$e->getCode().$e->getMessage()."<br />".
    in .$e->getFile(). on line $e->getLine(). "<br />";
}

?>
<?php /** * SQLite数据库操作类库 * 功能:连接管理、CRUD操作、事务支持、预处理语句 */ class SQLiteDB { private $db_path; private $pdo; private $error; /** * 构造函数 - 创建数据库连接 * @param string $db_path 数据库文件路径 */ public function __construct($db_path) { $this->db_path = $db_path; try { // 创建PDO连接(SQLite是文件型数据库) $this->pdo = new PDO("sqlite:" . $this->db_path); // 设置错误模式为异常 $this->pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION); // 启用外键支持 $this->pdo->exec("PRAGMA foreign_keys = ON"); } catch (PDOException $e) { $this->error = $e->getMessage(); throw new Exception("数据库连接失败: " . $this->error); } } // private static function instance() // { // if (!self::$db) { // self::$db = new SQLiteDB(); // } // } public function prepare($sql) { return $this->pdo->prepare($sql); } public function prepareAndExecute($sql, $data = []) { if (!empty($data)) { // 参数绑定和执行 $stmt = $this->pdo->prepare($sql); foreach ($data as $key => &$value) { switch (gettype($value)) { case 'integer': $stmt->bindValue(":" . $key, $value, PDO::PARAM_INT); break; case 'double': $stmt->bindValue(":" . $key, $value, PDO::PARAM_STR); break; default: $stmt->bindValue(":" . $key, $value, PDO::PARAM_STR); } } return $stmt->execute(); } else { // 直接执行 SQL 查询 return $this->pdo->exec($sql); } } /** * 执行查询语句(返回结果集) * @param string $sql SQL语句(支持预处理占位符) * @param array $params 绑定参数数组 * @return PDOStatement 结果集对象 */ // public function query($sql, $params = []) { // try { // $stmt = $this->pdo->prepare($sql); // $stmt->execute($params); // return $stmt; // } catch (PDOException $e) { // throw new Exception("查询执行失败: " . $e->getMessage()); // } // } /** * 执行更新操作(增删改) * @param string $sql SQL语句 * @param array $params 绑定参数数组 * @return int 受影响的行数 */ public function execute($sql, $params = []) { try { $stmt = $this->pdo->prepare($sql); $stmt->execute($params); return $stmt->rowCount(); } catch (PDOException $e) { throw new Exception("操作执行失败: " . $e->getMessage()); } } /** * 查询单个字段 * @param string $sql * @return void|string */ // public static function querySingle($sql) // { // self::instance(); // $result = @self::$db->querySingle($sql); // return $result ? $result : ''; // } /** * 获取单行数据获取一行结果 * @param string $sql SQL语句 * @param array $params 绑定参数数组 * @return array 单行数据数组 */ // public function fetchRow($sql, $params = []) { // self::instance(); // $result = @self::$db->querySingle($sql, true); // return $result; // } public function fetchRow($sql) { // 获取一行结果 $stmt = $this->pdo->query($sql); if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) { return $row; } return null; } /** * 获取全部数据 * @param string $sql SQL语句 * @param array $params 绑定参数数组 * @return array 二维数据数组 */ public function fetchAll($sql) { $stmt = $this->pdo->query($sql); return $stmt->fetchAll(PDO::FETCH_ASSOC); } /** * 获取最后插入的ID * @return string 最后插入行的ID */ public function lastInsertId() { return $this->pdo->lastInsertId(); } /** * 开启事务 */ public function beginTransaction() { $this->pdo->beginTransaction(); } /** * 提交事务 */ public function commit() { $this->pdo->commit(); } /** * 回滚事务 */ public function rollBack() { $this->pdo->rollBack(); } 完善操作sqlite数据库的类库
07-15
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值