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的更多相关文章

  1. Java 异常处理 try catch finally throws throw 的使用和解读(一)

    //最近的一个内部表决系统开发过程中,//发现对异常处理还存在一些模棱两可的地方,//所以想着整理一下//主要涉及到://1.try catch finally throws throw 的使用和解读 ...

  2. java异常处理try catch finally

    1       异常 1.1      异常处理的作用 在编程时,如果出现文件打开失败,读写文件就会异常退出.如果出现内存溢出错误,程序也会异常退出.如果不能对这些异常进行处理.程序则无法正常运行.所 ...

  3. 异常处理 try...catch...finally 执行顺序, 以及对返回值得影响

    异常处理 try...catch...finally 执行顺序, 以及对返回值得影响 结论:1.不管有没有出现异常,finally块中代码都会执行:2.当try和catch中有return时,fina ...

  4. C++异常处理: try,catch,throw,finally的用法

    写在前面 所谓异常处理,即让一个程序运行时遇到自己无法处理的错误时抛出一个异常,希望调用者可以发现处理问题. 异常处理的基本思想是简化程序的错误代码,为程序键壮性提供一个标准检测机制. 也许我们已经使 ...

  5. C#-异常处理:tyr,catch,finally ---ShinePans

    异常处理能够解决诸如一下问题: 数据库连接失败,IO错误,数据溢出,数组下表越界等问题.  总结:我认为在某些easy出错的地方加上 异常处理语句是很明智的选择 finally 是不管怎样都要运行的语 ...

  6. js中的异常处理try...catch使用介绍

    在JavaScript可以使用try...catch来进行异常处理. 例如: try { foo.bar();} catch (e) { alert(e.name + ": " + ...

  7. javascript 之异常处理try catch finally--05

    语法结构 try catch finally是ECMAScript-262 第三版提供异常处理机制的标准,语法结构如下: try{ //可能会发生的错误代码 } catch(error){ //错误处 ...

  8. 异常处理-try catch

    一:try catch是什么 try catch是java程序设计中处理异常的重要组成部分 异常是程序中的一些错误,有些异常需要做处理,有些则不需要捕获处理,异常是针对方法来说的,抛出.声明抛出.捕获 ...

  9. js的异常处理 try catch

    <script language="JavaScript"> try { throw new Error(10,"asdasdasd") } cat ...

  10. Java中的异常处理try catch(第八周课堂示例总结)

    异常处理 使用Java异常处理机制: 把可能会发生错误的代码放进try语句块中. 当程序检测到出现了一个错误时会抛出一个异常对象. 异常处理代码会捕获并处理这个错误. catch语句块中的代码用于处理 ...

随机推荐

  1. 简单的mysql查询

    mysql是基于客户机-服务器的数据库.客户机-服务器应用分为两个不同的部分.服务器部分是负责所有数据访问和处理的一个软件. 连接mysql 要连接mysql需要知道如下 主机名: 本地为localh ...

  2. extern的用法

    extern作为外部函数声明的用法: 1. 可以扩展函数的应用范围: 107.h #ifndef _107H_ #def _107H_ extern void func(); #endif 107.c ...

  3. What is the difference between a binary tree, a binary search tree, a B tree and a B+ tree?

    Binary Tree : It is a tree data structure in which each node has at most two children. As such there ...

  4. js的动态加载、缓存、更新以及复用(三)

    总体思路 1.  建立一个js服务,该服务实现通用js文件的加载.依赖.缓存.更新以及复用. 2.  各个项目如果使用通用js,可(bi)以(xu)使用js服务实现加载. 3.  Js服务只提供通用的 ...

  5. CodeIgniter_2 路由中定义伪静态 直接映射到相关的控制器

    某些情况下 隐藏路径信息 使用伪静态定义: RewriteRule ^no/torrent(.*)$ /index.php/torrent/doit/$1 CodeIgniter会自动识别到 no 控 ...

  6. sublime安装

    1.sublime下载安装 下载地址:http://www.sublimetext.com/3 选择合适版本下载 2.自定义快捷键 Ctrl+Shift+P调出命令面板 3.自定义设置 Ctrl+Sh ...

  7. Navigator对象、Screen对象

    Navigator对象:         Window对象的navigator属性引用的是包含浏览器厂商和版本信息的Navigator对象:   Navigator对象集合:plugins[] 返回对 ...

  8. 深入源码分析使用jQuery连续发起jsonp请求失败的原因

    jQuery的 jsonp 大家应该是十分熟悉了.曾遇到过这样的需求1.希望请求几个相似的内容添加到页面2.请求的内容一定时间内是固定不变的,希望做个缓存. 于是脑子一拍写下了类似这样的代码 for( ...

  9. POI中操作PPT获得每页的TABLE

    HSLFSlideShow slideShow = new HSLFSlideShow(bufferInputUtil.getBufferedInputStream()); logger.info(& ...

  10. VirtualBox & Kaili Linux 环境搭建

    勾选启用PAE/NX 勾选固态驱动器 网卡设置 [添加源] 2016.1月版添加源 cat << EOF > /etc/apt/sources.list deb http://htt ...