简体   繁体   English

PHP-尝试捕获块(异常)处理

[英]PHP - try and catch block (Exception) handling

I'm in doubt about exception handling. 我对异常处理有疑问。

If I have a function foo(); 如果我有一个函数foo(); is it same as if I handle it like this: 是否就像我这样处理:

try {
    foo();
} catch (Exception $e) {
    // do someting
}

or if I do it in function like this: 或者如果我在这样的功能中这样做:

foo() {
    try {
       // function body
    } catch (Exception $e) {
       // do someting
    }
}

And what if I do not throw Exception ? 如果我不抛出Exception怎么办? Code will continue to execute even if error appears ? 即使出现错误,代码也会继续执行?

Yes, code will continue. 是的,代码将继续。 In php exists errors and exceptions. 在php中存在错误和异常。 You can handle errors with function set_error_handler() , handle uncathable exceptions with function set_exception_handler() and you can handle exceptions using try .. catch 您可以使用函数set_error_handler()处理错误,使用函数set_exception_handler()处理无法处理的异常,还可以使用try .. catch处理异常try .. catch

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM