Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/361.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何在未执行的代码中查找异常?_Python - Fatal编程技术网

Python 如何在未执行的代码中查找异常?

Python 如何在未执行的代码中查找异常?,python,Python,在这个简单的Python代码中,如果a的值从0更改,则会出现namererror显示b未定义。但是,如果从未执行else子句,则错误将保持隐藏状态。如何检测此类错误 a=0 if a==0 : print "hello" else : print b 能够在python代码中发现许多常见(但不太常见)错误,而无需执行,示例输出来自您的示例: C: 1, 0: Exactly one space required around assignment a=0 ^ (bad-whites

在这个简单的Python代码中,如果
a
的值从
0
更改,则会出现
namererror
显示
b
未定义。但是,如果从未执行
else
子句,则错误将保持隐藏状态。如何检测此类错误

a=0
if a==0 :
  print "hello"
else :
  print b
能够在python代码中发现许多常见(但不太常见)错误,而无需执行,示例输出来自您的示例:

C:  1, 0: Exactly one space required around assignment
a=0
 ^ (bad-whitespace)
C:  2, 0: Exactly one space required around comparison
if a==0 :
    ^^ (bad-whitespace)
C:  2, 0: No space allowed before :
if a==0 :
        ^ (bad-whitespace)
C:  4, 0: No space allowed before :
else :
     ^ (bad-whitespace)
C:  1, 0: Missing module docstring (missing-docstring)
C:  1, 0: Invalid constant name "a" (invalid-name)
E:  5,10: Undefined variable 'b' (undefined-variable)
>>> a = 1
>>> if a == 0:
...     print a
... else:
...     try:
...         print b
...     except Exception as e:
...         print 'Caught Exception: ', e  # where e is the exception string
...
Caught Exception:  name 'b' is not defined
这里您感兴趣的一行是:
E:5,10:未定义变量'b'(未定义变量)

能够在python代码中找到许多常见(不太常见)错误,而无需执行它,示例输出如下:

C:  1, 0: Exactly one space required around assignment
a=0
 ^ (bad-whitespace)
C:  2, 0: Exactly one space required around comparison
if a==0 :
    ^^ (bad-whitespace)
C:  2, 0: No space allowed before :
if a==0 :
        ^ (bad-whitespace)
C:  4, 0: No space allowed before :
else :
     ^ (bad-whitespace)
C:  1, 0: Missing module docstring (missing-docstring)
C:  1, 0: Invalid constant name "a" (invalid-name)
E:  5,10: Undefined variable 'b' (undefined-variable)
>>> a = 1
>>> if a == 0:
...     print a
... else:
...     try:
...         print b
...     except Exception as e:
...         print 'Caught Exception: ', e  # where e is the exception string
...
Caught Exception:  name 'b' is not defined

您感兴趣的行是:
E:5,10:未定义变量“b”(未定义变量)

有两种方法可以实现这一点:

  • 或者在块中包含这样的语句。例如:

    C:  1, 0: Exactly one space required around assignment
    a=0
     ^ (bad-whitespace)
    C:  2, 0: Exactly one space required around comparison
    if a==0 :
        ^^ (bad-whitespace)
    C:  2, 0: No space allowed before :
    if a==0 :
            ^ (bad-whitespace)
    C:  4, 0: No space allowed before :
    else :
         ^ (bad-whitespace)
    C:  1, 0: Missing module docstring (missing-docstring)
    C:  1, 0: Invalid constant name "a" (invalid-name)
    E:  5,10: Undefined variable 'b' (undefined-variable)
    
    >>> a = 1
    >>> if a == 0:
    ...     print a
    ... else:
    ...     try:
    ...         print b
    ...     except Exception as e:
    ...         print 'Caught Exception: ', e  # where e is the exception string
    ...
    Caught Exception:  name 'b' is not defined
    
    使用还可用于获取与引发的
    异常相关的所有信息

  • 如果您希望在函数范围内执行此行为,请创建一个装饰器,并将该装饰器与需要此行为的函数一起使用


  • 有两种方法可以实现这一点:

  • 或者在块中包含这样的语句。例如:

    C:  1, 0: Exactly one space required around assignment
    a=0
     ^ (bad-whitespace)
    C:  2, 0: Exactly one space required around comparison
    if a==0 :
        ^^ (bad-whitespace)
    C:  2, 0: No space allowed before :
    if a==0 :
            ^ (bad-whitespace)
    C:  4, 0: No space allowed before :
    else :
         ^ (bad-whitespace)
    C:  1, 0: Missing module docstring (missing-docstring)
    C:  1, 0: Invalid constant name "a" (invalid-name)
    E:  5,10: Undefined variable 'b' (undefined-variable)
    
    >>> a = 1
    >>> if a == 0:
    ...     print a
    ... else:
    ...     try:
    ...         print b
    ...     except Exception as e:
    ...         print 'Caught Exception: ', e  # where e is the exception string
    ...
    Caught Exception:  name 'b' is not defined
    
    使用还可用于获取与引发的
    异常相关的所有信息

  • 如果您希望在函数范围内执行此行为,请创建一个装饰器,并将该装饰器与需要此行为的函数一起使用


  • 如果
    a
    定义为
    a=0
    (如OP示例所示),pylint将捕获此类错误创建a以抑制错误并打印/写入以记录异常,
    else
    子句将永远不会执行如果
    a
    定义为
    a=0
    (如OP示例中所示),则将永远不会执行
    else
    子句