【问题标题】:Some times there is Traceback, and sometime there is not in Python有时有 Traceback,有时在 Python 中没有
【发布时间】:2020-12-31 12:51:03
【问题描述】:

我在 Python 解释器中乱搞,我意识到一件奇怪的事情。口译会议:

>>> some_var
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name 'some_var' is not defined
>>> "
  File "<stdin>", line 1
    "
     ^
SyntaxError: EOL while scanning string literal

我得到的第一个错误是因为 some_var 没有定义,第二个错误是因为它的语法无效。

在第一个中我得到Traceback (most recent call last):,但在第二个中我没有。这是为什么呢?

【问题讨论】:

  • A SyntaxError 导致您的代码无法编译(因为它的语法无效),它甚至无法运行,这就是为什么没有回溯

标签: python error-handling syntax-error


【解决方案1】:

当代码成功执行/编译时你会得到一个Traceback,当你没有得到Traceback时,它没有成功编译。

在示例中,它试图找到some_var,但它不存在,但仍然编译成功,因此它给出了Traceback。但是由于"没有编译成功所以没有Traceback

【讨论】:

    猜你喜欢
    • 2016-02-06
    • 2015-01-13
    • 1970-01-01
    • 2022-10-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-21
    相关资源
    最近更新 更多