我希望这不是一个天真的问题。以下是代码:
>>> print(55/0)
Traceback (most recent call last):
File "<interactive input>", line 1, in <module>
ZeroDivisionError: integer division or modulo by zero
我知道python有traceback对象,我猜这是一种异常对象。我的问题是:是什么意思
“in <module>
”在第二行?它是哪个模块?它总是在当前模块中意味着什么?它没有提供任何有用的信息,对吧?
非常感谢您的时间和关注。
答案 0 :(得分:0)
尝试下面的代码后,我终于明白了。
def get_age():
age = input("Please enter your age: ")
if not age.isdigit():
my_error = TypeError("{0} is not integer".format(age))
raise my_error
elif age < 0:
# Create a new instance of an exception
my_error = ValueError("{0} is not positive".format(age))
raise my_error
return age
def a():
get_age()
def b():
a()
def c():
b()
def d():
try:
c()
print("haha")
except ValueError:
print("no one has negative age, you fool")
#except TypeError:
#print("you should input an integer, dear")
d()
以下是我没有处理TypeError时的错误消息。
RESTART: the_path_to_my_python_file.py
Please enter your age: th
Traceback (most recent call last):
File "the_path_to_my_python_file.py", line 33, in <module>
d()
File "the_path_to_my_python_file.py", line 26, in d
c()
File "the_path_to_my_python_file.py", line 22, in c
b()
File "the_path_to_my_python_file.py", line 18, in b
a()
File "the_path_to_my_python_file.py", line 14, in a
get_age()
File "the_path_to_my_python_file.py", line 5, in get_age
raise my_error
TypeError: th is not integer
你知道它并没有告诉你&#34;在模块&#34;了。每次它告诉您发生错误的函数的名称。我认为这是模块中的那种信息&#34;&#34;会提供。