标签:回调 inverse function exce func ace webserver line error:
看看 try catch 的作用,try的作用就等同于运行,如果try里面的代码运行正常没用报错的话,将不会触发catch代码块,如果有的话(如下面的$x为0的时候,函数没有return回数值,那么将会报告)
Fatal error: Uncaught Exception: Division by zero. in D:\webserver\www\err.php:4 Stack trace: #0 D:\webserver\www\err.php(8): inverse(0) #1 {main} thrown in D:\webserver\www\err.php on line 4
但是下面的因为try去捕获错误仅仅输出为:
0.2 Caught exception: Division by zero. Hello World
example:
<?php function inverse($x) { if (!$x) { throw new Exception(‘Division by zero.‘); } return 1/$x; } try { echo inverse(5) . "\n"; echo inverse(0) . "\n"; } catch (Exception $e) { echo ‘Caught exception: ‘, $e->getMessage(), "\n"; } // Continue execution echo "Hello World\n"; ?>
try catch后面可以跟随finally为必须处理的回调
try { echo inverse(0) . "\n"; } catch (Exception $e) { echo ‘Caught exception: ‘, $e->getMessage(), "\n"; } finally { echo "Second finally.\n"; }
无论try里面的代码块是否会有error抛出都会输出
Second finally.\n
标签:回调 inverse function exce func ace webserver line error:
原文地址:http://www.cnblogs.com/xuweiqiang/p/7976075.html