异常处理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统计列表内元素个数
代码如下: list01 = ['a','b','c','a','c'] set01 = set(list01) print(set01) dict01 = {} for item in set01: ...
- Mac 连接阿里云服务器
1. 通过命令行连接 Server 并设置 1.1 连接 Server #: ssh root@hctec.top ssh: 远程连接工具 root: 远程服务器用户名, 此处我用的是: root 用 ...
- 设置文件为源文件(和src一样)
点击project->properties->java build path->source
- java基础盲点梳理
类的基本成员才有默认值 finalize()并非析构,Java中没有析构,使用finalize()通常在于跨语言调用情景:比如使用C进行malloc内存分配以后,要在finalize()方法中进行fr ...
- Could not publish to the server. java.lang.NullPointerException
右键单击tomcat服务器,找到Properties,点下switch location就好了.
- 数据库热备之SQLServer的数据库镜像实施笔记
/ 最初在为公司设计SQLServer数据库镜像的时候,首先考虑的是高可用性(三台计算机,一台见证服务器,一台做主数据库,一台做镜像) 在虚拟机环境下部署成功,一切都是那么的完美.故障转移3秒之内就可 ...
- Webform(Linq增删改查)
Linq高集成化的数据访问类,它会自动映射数据库结构,将表名完整映射成为类名,将列名完整映射成字段名数据库数据访问,能大大减少代码量.(1)Linq创建添加LINQ to SQL类,类名需与要连接的数 ...
- 已解决:Ubuntu php gettext 无效
使用gettext做多文字国际化,按PHP官方文档,在CentOS上OK,在Ubuntu上不行,调试了快1天,试了网上提供的无数方法,终于有一个OK了…… 看到此文的同学别再被坑了…… 感谢:http ...
- $('div a') 与$('div>a'),.div+.div2与.div~.div2
$('div a'):div标签下所有层次a元素的jquery对象 $('div>a'):div标签下子元素层次a元素的jquery对象 <body> <div class=' ...
- JavaScript基础17——js的Date对象
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title> ...