首页
技术小册
AIGC
面试刷题
技术文章
MAGENTO
云计算
视频课程
源码下载
PDF书籍
「涨薪秘籍」
登录
注册
PHP中的include和require
文件操作基础
fopen( )打开文件或URL
fread()函数
fclose()函数
fwrite( )函数
PHP文件上传
PHP中使用Cookie
PHP中使用Sessions
Filter和Filter常量
PHP中实现回调函数
PHP异常处理
当前位置:
首页>>
技术小册>>
PHP合辑2-高级进阶
小册名称:PHP合辑2-高级进阶
异常是程序本身可以处理的意外程序结果。PHP中的异常处理与所有编程语言中的异常处理几乎类似。 PHP为此提供了以下专门的关键字。 - try:它表示可能发生异常的代码块。 - catch:它表示在抛出特定异常时要执行的代码块。 - throw:它用于抛出异常。它也用于列出函数抛出但不处理的异常。 - finally:它用于代替catch块或catch块之后,基本上它是用于PHP代码的清理活动。 - 为什么要在PHP中进行异常处理? **异常处理相对于错误处理有以下主要优点:** 错误处理代码与普通代码分离:在传统的错误处理代码中,总是有一个if-else块来处理错误。这些条件和处理错误的代码混在一起,导致代码难以阅读。使用try-catch块,代码变得易于阅读。 错误类型的分组:在PHP中,基本类型和对象都可以作为异常抛出。它可以创建异常对象的层次结构,在命名空间或类中分组异常,并根据类型对它们进行分类。 示例: ``` <?php // PHP Program to illustrate normal // try catch block code function demo($var) { echo " Before try block"; try { echo "\n Inside try block"; // If var is zero then only if will be executed if($var == 0) { // If var is zero then only exception is thrown throw new Exception('Number is zero.'); // This line will never be executed echo "\n After throw (It will never be executed)"; } } // Catch block will be executed only // When Exception has been thrown by try block catch(Exception $e) { echo "\n Exception Caught", $e->getMessage(); } // This line will be executed whether // Exception has been thrown or not echo "\n After catch (will be always executed)"; } // Exception will not be raised demo(5); // Exception will be raised here demo(0); ?> ``` output ``` Before try block Inside try block After catch (will be always executed) Before try block Inside try block Exception CaughtNumber is zero. After catch (will be always executed) ``` 示例2 ``` <?php // PHP Program to illustrate normal // try catch block code function demo($var) { echo " Before try block"; try { echo "\n Inside try block"; // If var is zerothen only if will be executed if($var == 0) { // If var is zero then only exception is thrown throw new Exception('Number is zero.'); // This line will never be executed echo "\n After throw it will never be executed"; } } // Catch block will be executed only // When Exception has been thrown by try block catch(Exception $e) { echo "\n Exception Caught" . $e->getMessage(); } finally { echo "\n Here cleanup activity will be done"; } // This line will be executed whether // Exception has been thrown or not echo "\n After catch it will be always executed"; } // Exception will not be raised demo(5); // Exception will be raised here demo(0); ?> ``` output ``` Before try block Inside try block Here cleanup activity will be done After catch (will be always executed) Before try block Inside try block Exception CaughtNumber is zero. Here cleanup activity will be done After catch (will be always executed) ``` 使用自定义异常类 ``` <?php class myException extends Exception { function get_Message() { // Error message $errorMsg = 'Error on line '.$this->getLine(). ' in '.$this->getFile() .$this->getMessage().' is number zero'; return $errorMsg; } } function demo($a) { try { // Check if if($a == 0) { throw new myException($a); } } catch (myException $e) { // Display custom message echo $e->get_Message(); } } // This will not generate any exception demo(5); // It will cause an exception demo(0); ?> ``` output: ``` Error on line 20 in /home/45ae8dc582d50df2790517e912980806.php0 is number zero ``` 设置顶层异常处理程序:set_exception_handler()函数将所有未捕获的异常都设置为所有用户定义的函数。 示例: ``` <?php // PHP Program to illustrate normal // try catch block code // Function for Uncaught Exception function myException($exception) { // Details of Uncaught Exception echo "\nException: " . $exception->getMessage(); } // Set Uncaught Exception handler set_exception_handler('myException'); function demo($var) { echo " Before try block"; try { echo "\n Inside try block"; // If var is zero then only if will be executed if($var == 0) { // If var is zero then only exception is thrown throw new Exception('Number is zero.'); // This line will never be executed echo "\n After throw (it will never be executed)"; } } // Catch block will be executed only // When Exception has been thrown by try block catch(Exception $e) { echo "\n Exception Caught", $e->getMessage(); } // This line will be executed whether // Exception has been thrown or not echo "\n After catch (will be always executed)"; if($var < 0) { // Uncaught Exception throw new Exception('Uncaught Exception occurred'); } } // Exception will not be raised demo(5); // Exception will be raised here demo(0); // Uncaught Exception demo (-3); ?> ``` output: ``` Before try block Inside try block After catch (will be always executed) Before try block Inside try block Exception CaughtNumber is zero. After catch (will be always executed) Before try block Inside try block After catch (will be always executed) Exception: Uncaught Exception occurred ```
上一篇:
PHP中实现回调函数
该分类下的相关小册推荐:
Laravel(10.x)从入门到精通(十九)
PHP8入门与项目实战(6)
Magento零基础到架构师(内容设计)
Laravel(10.x)从入门到精通(十七)
HTTP权威指南
Laravel(10.x)从入门到精通(二)
Laravel(10.x)从入门到精通(三)
Laravel(10.x)从入门到精通(九)
Magento零基础到架构师(库存管理)
Laravel(10.x)从入门到精通(一)
Laravel(10.x)从入门到精通(十六)
PHP程序员面试算法宝典