我试图弄清楚PHP中的throw new Exception
后面的代码是否仍然执行 - 我已经尝试过了,它似乎没有输出任何内容,但是很想知道。'抛出新的异常'需要退出()?
17
A
回答
32
不,不执行抛出异常后的代码。
在此代码示例我用数字标记,其将被执行的行(码流):
try {
throw new Exception("caught for demonstration"); // 1
// code below inside the try{} block is never executed
echo "you won't read this." . PHP_EOL;
} catch (Exception $e) {
// you may want to react on the Exception here
echo "exception caught!" . PHP_EOL; // 2
}
// execution flow continues here, because Exception above has been caught
echo "yay, lets continue!" . PHP_EOL; // 3
throw new Exception("uncaught for demonstration"); // 4, end
// execution flow never reaches this point because of the Exception thrown above
// results in "Fatal Error: uncaught Exception ..."
echo "you won't see me, too" . PHP_EOL;
当一个异常被抛出后的语句,代码将不会被执行,并且PHP将尝试查找第一个匹配的catch块。如果未捕获到异常,除非已使用
set_exception_handler()
定义了处理程序,否则会发出PHP致命错误,并显示“Uncaught Exception ...”消息。
3
不,不会执行throw
语句后的代码。很像return
。
相关问题
- 1. 当类不需要抛出异常
- 2. SqlConnection.OpenAsync()退出而不抛出异常
- 3. 抛出或重新抛出异常?
- 4. 抛出一个新的异常抛出一个旧的异常
- 5. 抛出新的异常
- 6. 魔术异常抛出抛出异常
- 7. 抛出异常抛出异常
- 8. 抛出异常不抛出
- 9. 异常不需要被抛出,但IOException异常
- 10. 更新SQL抛出异常
- 11. 重新抛出异常
- 12. 异常重新抛出
- 13. 抛出异常
- 14. 异常抛出
- 15. 抛出异常
- 16. 抛出异常
- 17. 抛出异常
- 18. 抛出异常
- 19. 抛出异常
- 20. 抛出异常
- 21. 异常抛出异常
- 22. SherlockFragmentActivity抛出所需的回调异常
- 23. 上次抛出的异常包含以前抛出的异常?
- 24. 退出时出现异常
- 25. 运营商新的与抛出异常选项仍然抛出异常
- 26. 在Java中抛出异常的方法抛出异常
- 27. LLVM异常抛出
- 28. socket.connect抛出异常
- 29. 类抛出异常
- 30. os.getcwd()抛出异常
我不知道什么是异常的一点,如果它不会导致堆栈展开(直到适当的条件,例如'catch',阻止它展开堆栈)... – 2012-06-26 19:50:12
_When exception is抛出后,语句后面的代码将不会执行,并且PHP将尝试查找第一个匹配的catch块。如果没有捕获到异常,除非使用set_exception_handler()._ [来自文档](http:// php。)定义了一个处理程序,否则将发出一个PHP致命错误,并带有“Uncaught Exception ...”消息。净/手动/ EN/language.exceptions.php) –