PHP: 异常exception
异常最常见于SDK调用中,函数执行失败时抛出异常,顺带错误码和错误信息。
先来看下PHP的异常处理相关函数:
public Exception::__construct() ([ string $message = "" [, int $code = 0 [, Exception $previous = NULL ]]] ) 构造函数:第一个参数$message为错误信息,第二个参数$code为错误码,第三个参数$previous为前一个异常
函数组:
获取异常的错误码:
final public mixed Exception::getCode ( void )
获取异常的错误信息:
final public string Exception::getMessage ( void )
获取异常发生的文件信息:
final public string Exception::getFile ( void )
获取异常发生在文件的哪一行
final public int Exception::getLine ( void )
获取前一个异常信息:
final public Exception Exception::getPrevious ( void )
最常用的是getCode和getMessage函数。
实例1:
<?php
class test
{
public function run($num)
{
try{
$this->process($num);
} catch (Exception $e) {
echo sprintf("Errno:%d Error:%s", $e->getCode(), $e->getMessage());
}
} public function process($num)
{
switch($num) {
case 1: throw new Exception("error", $num);break;
default:return $num;
}
return;
}
} $app = new test();
$app->run(1);
输出:
Errno:1 Error:error
问题1:异常被抛出后之后的流程会不会执行?答案是:不会的,函数在抛出异常处返回,注意在获取异常处是会继续执行的。
实例2
<?php
class test
{
public function run($num)
{
try{
$this->process($num);
} catch (Exception $e) {
echo sprintf("Errno:%d Error:%s", $e->getCode(), $e->getMessage());
}
} public function process($num)
{
switch($num) {
case 1: throw new Exception("error", $num);echo "new exception\n";break;
default:return $num;
}
return;
}
} $app = new test();
$app->run(1);
输出结果:
Errno:1 Error:error
注意:throw之后的echo语句没有被执行。
关于更多Exception的主题说明:http://docs.oracle.com/javase/tutorial/essential/exceptions/advantages.html
PHP: 异常exception的更多相关文章
- Atitit java的异常exception 结构Throwable类
Atitit java的异常exception 结构Throwable类 1.1. Throwable类 2.StackTrace栈轨迹1 1.2. 3.cause因由1 1.3. 4.Suppres ...
- 05_Java异常(Exception)
1. 异常的概念 1.1什么是异常 异常指的是程序运行时出现的不正常情况. 1.2异常的层次 Java的异常类是处理运行时的特殊类,每一种异常对应一种特定的运行错误.所有Java异常类都是系统类库中E ...
- 异常Exception in thread "AWT-EventQueue-XX" java.lang.StackOverflowError
今天太背了,bug不断,检查到最后都会发现自己脑残了,粗心写错,更悲剧的是写错的时候还不提示错. 刚才有遇到一个问题,抛了这个异常Exception in thread "AWT-Event ...
- Sqoop异常:Exception in thread "main" java.lang.NoClassDefFoundError: org/json/JSONObject
18/12/07 01:09:03 INFO mapreduce.ImportJobBase: Beginning import of staffException in thread "m ...
- 异常 Exception 堆栈跟踪 异常捕获 MD
Markdown版本笔记 我的GitHub首页 我的博客 我的微信 我的邮箱 MyAndroidBlogs baiqiantao baiqiantao bqt20094 baiqiantao@sina ...
- 理解Python语言里的异常(Exception)
Exception is as a sort of structured "super go to".异常是一种结构化的"超级goto". 作为一个数十年如一日 ...
- PL/SQL 08 异常 exception
--PL/SQL错误 编译时 运行时 --运行时的出错处理 EXCEPTION --异常处理块DECLARE …BEGIN …EXCEPTION WHEN OTHERS THEN handle ...
- 【马克-to-win】学习笔记—— 第五章 异常Exception
第五章 异常Exception [学习笔记] [参考:JDK中文(类 Exception)] java.lang.Object java.lang.Throwable java.lang.Except ...
- 【异常】Maxwell异常 Exception in thread "main" net.sf.jsqlparser.parser.TokenMgrError: Lexical error at line 1, column 596. Encountered: <EOF> after : ""
1 详细异常 Exception in thread "main" net.sf.jsqlparser.parser.TokenMgrError: Lexical error at ...
- 异常-Exception in thread "main" net.sf.jsqlparser.parser.TokenMgrError: Lexical error at line 1, column 596. Encountered: <EOF> after :
1 详细异常 Exception in thread "main" net.sf.jsqlparser.parser.TokenMgrError: Lexical error at ...
随机推荐
- java.net.ConnectException connect refured
原因:tomcat 连接拒绝:tomcat没有完全重启 只是部分重启 解决方案: 连接tomcat服务 命令:1:ps -ef|grep java : 2:kill -9 21060 3:查看tomc ...
- Hive中频繁报警的问题
在使用Hive的过程中,是不是会在shell中报一堆警告,虽然说不影响正常使用,但是看着很烦人,而且指不定会影响数据的准确性和运行的稳定性. 警告的内容如下: Tue Aug :: CST WARN: ...
- hdu 1217 Arbitrage
Flody多源最短路 #include<cstdio> #include<cstring> #include<string> #include<cmath&g ...
- L3-003. 社交集群
L3-003. 社交集群 题目链接:https://www.patest.cn/contests/gplt/L3-003 查并集 与L2-007(家庭房产)类似,都是采用了并查集的算法,相对来说这题处 ...
- 《Mastering Opencv ...读书笔记系列》车牌识别(II)
http://blog.csdn.net/jinshengtao/article/details/17954427 <Mastering Opencv ...读书笔记系列>车牌识别(I ...
- redis :初步使用
redis : 1.ubuntu安装 'pip install redis-server' 2.启动 'redis-cli' 3.使用 set: set a 1 get: get a fl ...
- javascript基础(五)函数
原文http://pij.robinqu.me/ 通过call和apply间接调用函数(改变this) call 和 apply带有多个参数,call和apply把当前函数的this指向第一个参数给定 ...
- 在Wamp 添加站点和域名
在httpd.conf中使Include conf/extra/httpd-vhosts.conf生效 在conf/extra/httpd-vhosts.conf中加入如下代码 <Virtual ...
- hdu_5788_Level Up(树状数组+主席树)
题目链接:hdu_5788_Level Up 题意: 有一棵树,n个节点,每个节点有个能力值A[i],mid[i],mid的值为第i节点的子树的中位数(包括本身),现在让你将其中的一个节点的A值改为1 ...
- digitalocean解释:private networking和user data、IPv6是什么意思
digitalocean vps后台新建droplet的时候,底部会有available settings,里面有四个选项,大多数用户不懂啥意思,我今天解释下: Private Networking ...