简体   繁体   English

在python中放置异常处理的位置

[英]Where to put exception handling in python

When using try/except blocks in Python, is there a recommendation to delegate it to any methods that might raise an exception, or to catch it in the parent function, or both? 在Python中使用try / except块时,是否建议将其委托给可能引发异常的任何方法,或者在父函数中捕获它,或者两者兼而有之?

For example, which of the following is preferred? 例如,以下哪项是首选?

def my_function():
    s = something.that.might.go_wrong()
    return s

def main():
    try:
        s = my_function()
    except Exception:
        print "Error"

or 要么

def my_function():
    try:
        s = something.that.might.go_wrong()
        return s
    except Exception:
        print "Error"

def main():
    s = my_function()

PEP 8 seems to be quiet on the matter, and I seem to find examples of both cases everywhere. PEP 8在这个问题上似乎很安静,我似乎找到了两个案例的例子。

It really depends on the semantics of the functions in question. 它实际上取决于所讨论函数的语义。 In general if you're writing a library, your library probably should handle exceptions that get raised inside the library (optionally re-raising them as new library-specific exceptions). 通常,如果您正在编写库,那么您的库可能应该处理在库中引发的异常(可选地将它们重新提升为新的库特定异常)。

At the individual function level, though, the main thing you want to think about is what context/scope you desire to handle the exception in - if there is a reasonable different thing you could do in exceptional cases within the inner function, it might be useful to handle it within the inner function; 但是,在单个函数级别,您要考虑的主要事项是您希望处理异常的上下文/范围 - 如果在内部函数内的特殊情况下可以执行合理的不同操作,则可能是在内部函数中处理它很有用; otherwise, it might make more sense to handle it in the outer function. 否则,在外部函数中处理它可能更有意义。

For the specific case of writing output, it's often useful to only do that at the highest level, and inner functions only ever (a) return values or (b) raise exceptions. 对于编写输出的特定情况,仅在最高级别执行此操作通常很有用,而内部函数仅用于(a)返回值或(b)引发异常。 That makes the code easier to test because you don't have to worry about testing side effect output. 这使得代码更容易测试,因为您不必担心测试副作用输出。

If you are following the rule of "one function should handle one task" then You shouldn't handle exception in that function, Let it fail loudly on unexpected input. 如果您遵循“一个函数应该处理一个任务”的规则,那么您不应该在该函数中处理异常,让它在意外输入时大声失败。 Parent function which calling such function may handle to give better experience to user. 调用这种功能的父功能可以处理给用户更好的体验。

We can refer python builtin functions to get pythonic way 我们可以参考python内置函数来获得pythonic方式

I always use the second one. 我总是使用第二个。 Logically to me it seems that the problems of a function should be dealt with in that function only. 从逻辑上讲,似乎只能在该函数中处理函数的问题。 This would provide user a clean and a hassle free interface, so you could later put your code in a library. 这将为用户提供干净且无障碍的界面,因此您可以稍后将代码放入库中。

There could be some cases where you would want to use exceptions outside the function. 在某些情况下,您可能希望在函数外部使用异常。 For example if you want to print a particular message when something goes wrong then you should use the exception out of the function. 例如,如果您想在出现问题时打印特定消息,那么您应该使用该函数之外的异常。

However you could provide the exception statements as a argument to the function if you want to give user the ability to decide his own exception message. 但是,如果要让用户能够决定自己的异常消息,则可以将异常语句作为函数的参数提供。 So i guess the Second example(exception inside the function) would be universal and thus should be preferred. 所以我猜第二个例子(函数内部的例外)将是通用的,因此应该是首选。

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

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