异常处理try-catch-finally
php5.5新增 Finally模块
try {
//好好干,出了问题不要怕,外面有人接应
} catch (HttpException $e) {
//时刻准备着,处理上面抛出的HTTP问题
} catch (Exception $e) {
//时刻准备着,处理他们都处理不了的问题
} finally {
//打扫战场,都收拾好了再走人
}
try 中 return 后 finally 会继续执行,如果 finally 中也有return,则最终返回值为 finally 中 return 的值。
try 中 die 或 exit 后 finally 不会执行。
example01:
<?php
/**
finally块是个很好的设计,其中的return语句能覆盖其他块中的return语句,并处理try catch抛出的异常
无需try-catch嵌套来处理子try-catch抛出的异常
这个跟java的一样,c#是在finally块中存在return语句会抛出compile time error(编译时错误)
*/ function asdf()
{
try {
throw new Exception('error');
}
catch(Exception $e) {
echo "An error occurred";
throw $e;
}
finally {
//This overrides the exception as if it were never thrown
return "\nException erased";
}
} try {
echo asdf();
}
catch(Exception $e) {
echo "\nResult: " . $e->getMessage();
} /*
The output from above will look like this: An error occurred
Exception erased Without the return statement in the finally block it would look like this: An error occurred
Result: error
*/
example02:
<?php
/**
有个相悖的行为在PHP 5.5.3's finally 和 return statements:
在一个方法中,try块单返回一个变量,finally块修改这个变量,返回的是finally修改过的,
但当try块返回的变量参与运算(evaluated in-line),会忽略finally块对这个变量的修改
(不知道原因...)
*/
function returnVariable(){
$foo = 1;
try{
return $foo;
} finally {
$foo++;
}
} function returnVariablePlusZero(){
$foo = 1;
try{
return $foo+0;
} finally {
$foo++;
}
} $test1 = returnVariable(); // returns 2, not the correct value of 1.
$test2 = returnVariablePlusZero(); // returns correct value of 1, but inconsistent with $test1.
example03:
<?php
/**
小例子 验证变量
check if the name contains only letters, and does not contain the word name
*/ $name = "Name";
try
{
try
{
//preg_match() 返回 pattern 的匹配次数。 它的值将是0次(不匹配)或1次,因为 preg_match() 在第一次匹配后 将会停止搜索。 preg_match_all() 不同于此,它会一直搜索 subject 直到到达结尾。 如果发生错误 preg_match() 返回 FALSE 。
if(preg_match('/[^a-z]/i', $name))
{
throw new Exception("$name contains character other than a-z A-Z");
}
if(strpos(strtolower($name), 'name') !== false)
{
throw new Exception("$name contains the word name");
}
echo "The Name is valid";
}
catch (exception $e)
{
throw new Exception("insert name again", 0, $e);
}
} catch (exception $e)
{
if($e->getPrevious())
{
echo "The Previous Exception is: " . $e->getPrevious()->getMessage() . "<br/>";
}
echo "The Exception is: " . $e->getMessage() . "<br/>";
}
example04
<?php
/*
When catching an exception inside a namespace it is important that you escape to the global space:
如何逃离出命名空间
*/ namespace SomeNamespace; class SomeClass { function SomeFunction() {
try {
throw new Exception('Some Error Message');
} catch (\Exception $e) {
var_dump($e->getMessage());
}
} } //报错:
//Fatal error: Class 'SomeNamespace\Exception' not found in C:\xampp\htdocs\tonglei\index.php on line 8
example05
<?php //下面的写法会报T_THROW Syntax Error.
someFunction() OR throw new Exception(); //这种写法可以用下面这个正确的形式
function throwException($message = null,$code = null) {
throw new Exception($message,$code);
} someFunction() OR throwException();
异常处理try-catch-finally的更多相关文章
- Java 异常处理 try catch finally throws throw 的使用和解读(一)
//最近的一个内部表决系统开发过程中,//发现对异常处理还存在一些模棱两可的地方,//所以想着整理一下//主要涉及到://1.try catch finally throws throw 的使用和解读 ...
- java异常处理try catch finally
1 异常 1.1 异常处理的作用 在编程时,如果出现文件打开失败,读写文件就会异常退出.如果出现内存溢出错误,程序也会异常退出.如果不能对这些异常进行处理.程序则无法正常运行.所 ...
- 异常处理 try...catch...finally 执行顺序, 以及对返回值得影响
异常处理 try...catch...finally 执行顺序, 以及对返回值得影响 结论:1.不管有没有出现异常,finally块中代码都会执行:2.当try和catch中有return时,fina ...
- C++异常处理: try,catch,throw,finally的用法
写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...
- C#-异常处理:tyr,catch,finally ---ShinePans
异常处理能够解决诸如一下问题: 数据库连接失败,IO错误,数据溢出,数组下表越界等问题. 总结:我认为在某些easy出错的地方加上 异常处理语句是很明智的选择 finally 是不管怎样都要运行的语 ...
- js中的异常处理try...catch使用介绍
在JavaScript可以使用try...catch来进行异常处理. 例如: try { foo.bar();} catch (e) { alert(e.name + ": " + ...
- javascript 之异常处理try catch finally--05
语法结构 try catch finally是ECMAScript-262 第三版提供异常处理机制的标准,语法结构如下: try{ //可能会发生的错误代码 } catch(error){ //错误处 ...
- 异常处理-try catch
一:try catch是什么 try catch是java程序设计中处理异常的重要组成部分 异常是程序中的一些错误,有些异常需要做处理,有些则不需要捕获处理,异常是针对方法来说的,抛出.声明抛出.捕获 ...
- js的异常处理 try catch
<script language="JavaScript"> try { throw new Error(10,"asdasdasd") } cat ...
- Java中的异常处理try catch(第八周课堂示例总结)
异常处理 使用Java异常处理机制: 把可能会发生错误的代码放进try语句块中. 当程序检测到出现了一个错误时会抛出一个异常对象. 异常处理代码会捕获并处理这个错误. catch语句块中的代码用于处理 ...
随机推荐
- Python Sqlite3以字典形式返回查询结果
sqlite3本身并没有像pymysql一样原生提供字典形式的游标. cursor = conn.cursor(pymysql.cursors.DictCursor) 但官方文档里已经有预留了相应的实 ...
- 原博客地址http://blog.chinaunix.net/uid/20656672.html弃用
原博客地址http://blog.chinaunix.net/uid/20656672.html弃用
- 25款响应式,支持视网膜显示的 Wordpress 主题
响应式和现代设计风格的多用途 WordPress 主题与能够非常灵活的适应所有设备.而高级主题能够更大可能性的轻松定制.所有的主题是完全响应式的,您可以从主题选项中禁用/启用响应模式. 多用途的响应式 ...
- requirejs:让人迷惑的路径解析
接触过requirejs的童鞋可能都知道,无论是通过define来定义模块,还是通过require来加载模块,模块依赖声明都是很重要的一步.而其中涉及到的模块路径解析,对于新手来说,有的时候会让人觉得 ...
- Windows安装apache2.4
The primary Windows platform for running Apache 2.4 is Windows 2000 or later. Always obtain and inst ...
- 正则中关于修饰符g以及exec和match区别的一个小demo
代码: 输出结果 补充: reg.lastIndex:下一次正则捕获的开始查找的索引位置 ->正则的懒惰性就是因为默认情况下lastIndex值都是0,我们不管执行几次exec,都是从字符串的开 ...
- JavaScript 开发者经常忽略或误用的七个基础知识点(转)
JavaScript 本身可以算是一门简单的语言,但我们也不断用智慧和灵活的模式来改进它.昨天我们将这些模式应用到了 JavaScript 框架中,今天这些框架又驱动了我们的 Web 应用程序.很多新 ...
- Snort - manual 笔记(五)
1.9 Miscellaneous 1.9.1 Running Snort as a Daemon 如果你想让Snort作为守护程序运行,你可以在最后加上 -D 选项.清注意如果你想通过发送一个 SI ...
- PHP美元符和花括号组合那些事—${${}}
双美元符+{}:${${variable}}是一种比较常见的用法,但是它的实现原理是什么呢?今天来探究一下: 提及这种用法,还得先说一下PHP的String类型php.net上指出,一个字符串可以用4 ...
- SharePoint 2013 初始化Ribbon选中Tab
SharePoint使用中,经常打开页面会有默认展开的Ribbon选项,有时这又不是我们需要的,所以我们就需要默认选中的项目,下面简单介绍下如何实现. 方法一 1.Dispform.aspx页面默认R ...