只捕获Python中的一些运行时错误

时间:2022-10-28 15:06:05

I'm importing a module which raises the following error in some conditions: RuntimeError: pyparted requires root access

我正在导入一个模块,在某些情况下会引发以下错误:RuntimeError:pyparted需要root访问权限

I know that I can just check for root access before the import, but I'd like to know how to catch this spesific kind of error via a try/except statement for future reference. Is there any way to differentiate between this RuntimeError and others that might be raised?

我知道我可以在导入之前检查root访问权限,但我想知道如何通过try / except语句捕获这种特殊类型的错误以供将来参考。有没有办法区分这个RuntimeError和其他可能引发的错误?

6 个解决方案

#1


I know that I can just check for root access before the import, but I'd like to know how to catch this spesific kind of error via a try/except statement for future reference. Is there any way to differentiate between this RuntimeError and others that might be raised?

我知道我可以在导入之前检查root访问权限,但我想知道如何通过try / except语句捕获这种特殊类型的错误以供将来参考。有没有办法区分这个RuntimeError和其他可能引发的错误?

If the error is caused by a specific condition, then I think the easiest way to catch the error is to test for the condition, and you can raise a more specific error yourself. After all the 'error' exists before the error is thrown, since in this case its a problem with the environment.

如果错误是由特定条件引起的,那么我认为捕获错误的最简单方法是测试条件,并且您可以自己引发更具体的错误。在抛出错误之前存在所有“错误”之后,因为在这种情况下它是环境问题。

I agree with those above - text matching on an error is kind of a terrifying prospect.

我同意上述内容 - 关于错误的文本匹配是一种可怕的前景。

#2


You can check attributes of the exception to differentiate from other possible RuntimeError exceptions. For example, re-raise the error if it does not match a predefined message text.

您可以检查异常的属性以区别于其他可能的RuntimeError异常。例如,如果错误与预定义的消息文本不匹配,请重新引发错误。

    try:
        import pypatred
    except RuntimeError,e:
        if e.message == 'RuntimeError: pyparted requires root access':
            return 'pyparted - no root access'
        raise

Of course, direct text comparison is just an example, you could search for included substrings or regular expressions.

当然,直接文本比较只是一个示例,您可以搜索包含的子字符串或正则表达式。

It is worth noting that the .message attribute of exceptions is deprecated starting with Python 2.6. You can find the text in .args, usually args[0].

值得注意的是,从Python 2.6开始,不推荐使用异常的.message属性。您可以在.args中找到文本,通常是args [0]。

... For 2.6, the message attribute is being deprecated in favor of the args attribute.

...对于2.6,不推荐使用message属性,而使用args属性。

#3


try:
    import pyparted
except RuntimeError:
    print('RuntimeError is raised')
    raise

more on exception handling in tutorial.

更多关于教程中的异常处理。

This situation should produce ImportError in my opinion. And you can do it yourself:

在我看来,这种情况应该产生ImportError。你可以自己做:

try:
    import pyparted
except RuntimeError as e:
    raise ImportError(e)

#4


Yes.

   try:
        import module
   except RuntimeError:
         pass

imports are interpreted as any other statement, they are not special. You could do an

导入被解释为任何其他声明,它们并不特殊。你可以做一个

if condition:
     import module

#5


try:
   import ...
except RuntimeError:
   # Do something

#6


RuntimeError Raised when an error is detected that doesn’t fall in any of the other categories

RuntimeError在检测到不属于任何其他类别的错误时引发

def foo():
   try:
      foo()
   except RuntimeError, e:
      print e
      print " Runtime Error occurred due to exceeded maximum recursion depth "

That is how we will catch the RuntimeError caused by the exceeded recursion limit in python

这就是我们将如何捕获由python中超出的递归限制引起的RuntimeError

And if you want to call your function over the recursion limit you can do the following

如果要在递归限制上调用函数,可以执行以下操作

import sys
def foo():
     try:
        foo()
     except RuntimeError, e:
        sys.setrecursionlimit(1200)
        foo()

But always it is highly not recommended to change the recursion limit, but very small changes in the recursion limit are allowed

但总是不建议更改递归限制,但允许递归限制的非常小的更改

#1


I know that I can just check for root access before the import, but I'd like to know how to catch this spesific kind of error via a try/except statement for future reference. Is there any way to differentiate between this RuntimeError and others that might be raised?

我知道我可以在导入之前检查root访问权限,但我想知道如何通过try / except语句捕获这种特殊类型的错误以供将来参考。有没有办法区分这个RuntimeError和其他可能引发的错误?

If the error is caused by a specific condition, then I think the easiest way to catch the error is to test for the condition, and you can raise a more specific error yourself. After all the 'error' exists before the error is thrown, since in this case its a problem with the environment.

如果错误是由特定条件引起的,那么我认为捕获错误的最简单方法是测试条件,并且您可以自己引发更具体的错误。在抛出错误之前存在所有“错误”之后,因为在这种情况下它是环境问题。

I agree with those above - text matching on an error is kind of a terrifying prospect.

我同意上述内容 - 关于错误的文本匹配是一种可怕的前景。

#2


You can check attributes of the exception to differentiate from other possible RuntimeError exceptions. For example, re-raise the error if it does not match a predefined message text.

您可以检查异常的属性以区别于其他可能的RuntimeError异常。例如,如果错误与预定义的消息文本不匹配,请重新引发错误。

    try:
        import pypatred
    except RuntimeError,e:
        if e.message == 'RuntimeError: pyparted requires root access':
            return 'pyparted - no root access'
        raise

Of course, direct text comparison is just an example, you could search for included substrings or regular expressions.

当然,直接文本比较只是一个示例,您可以搜索包含的子字符串或正则表达式。

It is worth noting that the .message attribute of exceptions is deprecated starting with Python 2.6. You can find the text in .args, usually args[0].

值得注意的是,从Python 2.6开始,不推荐使用异常的.message属性。您可以在.args中找到文本,通常是args [0]。

... For 2.6, the message attribute is being deprecated in favor of the args attribute.

...对于2.6,不推荐使用message属性,而使用args属性。

#3


try:
    import pyparted
except RuntimeError:
    print('RuntimeError is raised')
    raise

more on exception handling in tutorial.

更多关于教程中的异常处理。

This situation should produce ImportError in my opinion. And you can do it yourself:

在我看来,这种情况应该产生ImportError。你可以自己做:

try:
    import pyparted
except RuntimeError as e:
    raise ImportError(e)

#4


Yes.

   try:
        import module
   except RuntimeError:
         pass

imports are interpreted as any other statement, they are not special. You could do an

导入被解释为任何其他声明,它们并不特殊。你可以做一个

if condition:
     import module

#5


try:
   import ...
except RuntimeError:
   # Do something

#6


RuntimeError Raised when an error is detected that doesn’t fall in any of the other categories

RuntimeError在检测到不属于任何其他类别的错误时引发

def foo():
   try:
      foo()
   except RuntimeError, e:
      print e
      print " Runtime Error occurred due to exceeded maximum recursion depth "

That is how we will catch the RuntimeError caused by the exceeded recursion limit in python

这就是我们将如何捕获由python中超出的递归限制引起的RuntimeError

And if you want to call your function over the recursion limit you can do the following

如果要在递归限制上调用函数,可以执行以下操作

import sys
def foo():
     try:
        foo()
     except RuntimeError, e:
        sys.setrecursionlimit(1200)
        foo()

But always it is highly not recommended to change the recursion limit, but very small changes in the recursion limit are allowed

但总是不建议更改递归限制,但允许递归限制的非常小的更改