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 ...
随机推荐
- struts2.3 创建工程
1:在该网站下载struts2.3.16.3,目前为最新版.http://www.struts.apache.org/download.cgi 不妨下载“Full Distribution”版本 下载 ...
- android -- 加载gif 防止oom
项目中涉及到gif图片的展示,原来使用的是gifview,但是当频繁的,加载过大图片的时候会出现OOM的问题,后来去github上面找相关的库: https://github.com/koral--/ ...
- nginx: [emerg] socket() [::]:80 failed (97: Address family not supported by protocol)
环境:Centos6.5 行为:安装nginx 问题: nginx: [emerg] socket() [::]: failed (: Address family not supported by ...
- Chapter 2 Open Book——31
"It's too bad about the snow, isn't it?" Edward asked. I had the feeling that he was forci ...
- 0. Java开发中的23种设计模式详解(转)
设计模式(Design Patterns) ——可复用面向对象软件的基础 设计模式(Design pattern)是一套被反复使用.多数人知晓的.经过分类编目的.代码设计经验的总结.使用设计模式是为了 ...
- icheck.min.js 选中效果
遍历所有 checkbox 如果是选中的用 绿色 如果未选中用 灰色 //check控件属性 $('input').each(function() { var self = $(this); var ...
- 2016NEFU集训第n+3场 D - Bicycle Race
Description Maria participates in a bicycle race. The speedway takes place on the shores of Lake Luc ...
- Gentoo本地化设置--时区、时钟、字体、中文环境
时区 你需要选择时区让系统知道你的地理位置,以保持正确的时间.在/usr/share/zoneinfo中查找你的时区.然后在/etc/conf.d/clock中设置时区.请忽略/usr/share/z ...
- Rational Rose 2003 逆向工程转换C++ / VC++ 6.0源代码成UML类图
目录 1.安装&破解Rational Rose 2003 1.1 安装Rose 2003 1.2 破解Rose 2003 1.3运行出错“没有找到suite objects.dl” 2. Ra ...
- 【笔记】Loadrunner添加OS类型为Windows的服务器(Win7)
最近在学习Loadrunner,看到“监控Windows资源”,决定小试一把,由于没有找到合适的镜像,暂时没有搞好Windows的虚拟机,so 先用自己小试牛刀了只有,不过这样子好像难度锐减也~只要小 ...