在python的try-exception
块中,如下图所示,我希望打印我的帮助消息,而不是python自己的错误消息。这可能吗?你知道吗
def genpos(a):
''' Generate POSCAR :
Some error message'''
try:
tposcar = aio.read(os.path.join(root,"nPOSCAR"))
cell = (tposcar.get_cell())
cell[0][0] = a
cell[1][1] = math.sqrt(3)*float(a)
tposcar.set_cell(cell, scale_atoms=True)
aio.write("POSCAR", tposcar, direct=True)
except:
help(genpos)
sys.exit()
所以,比方说,当在没有参数的情况下调用此代码时,我希望得到“Generate POSCAR:Some error message”,而不是python的 回溯(最近一次呼叫):
File "submit.py", line 41, in <module>
main()
File "submit.py", line 36, in __init__
ase_mod.genpos()
TypeError: genpos() missing 1 required positional argument: 'a'
你可以define a new exception:
不过,您收到的错误与
try-except
语句无关。相反,gen_pos()
函数缺少一个参数。你知道吗相关问题 更多 >
编程相关推荐