简体   繁体   English

什么是PHP相当于Python的尝试:...除外:

[英]What is the PHP equivalent to Python's Try: … Except:

I am a strong Python programmer, but not quite there when it comes to PHP. 我是一个强大的Python程序员,但在PHP方面并不完全。 I need to try something, and if that doesn't work out, do something else. 我需要尝试一些东西,如果不能解决问题,那就做点别的了。


This is what it would look like in Python: 这就是Python中的样子:

try:
      print "stuf"
except:
      print "something else"

What would this be in PHP? 这在PHP中会是什么?

http://php.net/manual/en/language.exceptions.php http://php.net/manual/en/language.exceptions.php

try {
    print 'stuff';
} catch (Exception $e) {
    var_dump($e);
}

Note: this only works for exceptions, not errors. 注意:这仅适用于异常,而不适用于错误。

See http://www.php.net/manual/en/function.set-error-handler.php for that. 请参阅http://www.php.net/manual/en/function.set-error-handler.php

try {

    // do stuff ...

} catch (Exception $e) {

    print($e->getMessage());

}

See http://php.net/manual/en/language.exceptions.php 请参见http://php.net/manual/en/language.exceptions.php

PHP does not natively support error catching like Python does, unless you override the default behavior and set your own error handler . PHP本身不像Python那样支持错误捕获,除非您覆盖默认行为并设置自己的错误处理程序 PHP's try - catch was only recently added to the language in version 5, and it can only catch exceptions you explicitly throw . PHP的try - catch最近才被添加到版本5中的语言中,它只能捕获您明确throw异常。

So basically, PHP distinguishes between errors and exceptions. 基本上,PHP区分错误和异常。 Errors haven't been modularized and made available to the user like they have been in Python. 错误没有模块化,并且像Python一样可供用户使用。 I believe that's related to the fact that PHP began as a collection of dynamic web scripts , grew and gained more features over time, and only more recently offered improved OOP support (ie, version 5); 我认为这与PHP最初是作为动态Web脚本集合开始,随着时间的推移而增长并获得更多功能这一事实有关,并且最近才提供改进的OOP支持(即版本5); whereas Python fundamentally supports OOP and other meta-functionality. 而Python从根本上支持OOP和其他元功能。 And exception handling from the beginning . 从一开始就处理异常。

Here's an example usage (again, a throw is necessary, or else nothing will be caught): 这是一个示例用法(同样, throw是必要的,否则什么都不会被捕获):

function oops($a)
{
    if (!$a) {
        throw new Exception('empty variable');
    }
    return "oops, $a";
}

try {
    print oops($b);
} catch (Exception $e) {
    print "Error occurred: " . $e->getMessage();
}

You can handle PHP errors like they were exceptions by using set_error_handler 您可以使用set_error_handler处理PHP错误,就像它们是异常一样

In this error handler function you can throw various exception, according to error level for instance. 在此错误处理函数中,您可以根据错误级别抛出各种异常。

By doing this you can treat any error (including programming errors) in a common way. 通过这样做,您可以以常见方式处理任何错误(包括编程错误)。

PHP 5 has the exception model : PHP 5有异常模型

try {
    print 'stuff';
} catch (Exception $e) {
    echo 'Caught exception: ',  $e->getMessage(), "\n";
}

Assuming you're trying to catch exceptions, take a look at http://php.net/manual/en/language.exceptions.php 假设您正在尝试捕获异常,请查看http://php.net/manual/en/language.exceptions.php

You could try something like 你可以试试像

try {
echo "Stuff";
} catch (Exception $e) {
echo "Something Else";
}

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

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