try catch throw

try {
  var_dump('abc');
  throw new Exception('abc');
  var_dump('abc-1');
}
catch(Exception $e){
  var_dump('catch');
}
var_dump('below try catch');
----------------
string(3) "abc"
string(5) "catch"
string(15) "below try catch"
---
是正常输出的, 在try里面的throw直接跳到exception, 并且exceptlin里面必须加参数, 然后跳出catch,继续运行,
但如果throw不在try里面,则直接报错了
  var_dump('abc');
  throw new Exception('abc');
  var_dump('abc-1');
var_dump('below try catch');

-------------------------------------------------------------------------------------------

string(3) "abc"

Fatal error: Uncaught Exception: abc in /box/script.php:4
Stack trace:
#0 {main}
  thrown in /box/script.php on line 4

Exited with error status 255

posted @ 2020-05-25 13:31  qqisnow2021  阅读(151)  评论(0编辑  收藏  举报