在不满足条件的情况下打印错误消息而不打印回溯并关闭程序 [英] Print an error message without printing a traceback and close the program when a condition is not met

查看:85
本文介绍了在不满足条件的情况下打印错误消息而不打印回溯并关闭程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我看到了与此类似的问题,但没有一个真正解决引用问题。
如果我有这样的课程

I've seen similar questions to this one but none of them really address the trackback. If I have a class like so

class Stop_if_no_then():
    def __init__(self, value one, operator, value_two, then, line_or_label, line_number):
        self._firstvalue = value_one
        self._secondvalue = value_two
        self._operator = operator
        self._gohere = line_or_label
        self._then = then
        self._line_number = line_number

    def execute(self, OtherClass):
        "code comparing the first two values and making changes etc"

我希望我的execute方法能够执行的操作是self._then不等于字符串 THEN(全部用大写字母表示),然后我希望它引发自定义错误消息并终止整个程序,同时也不会显示回溯。

What I want my execute method to be able to do is if self._then is not equal to the string "THEN" (in allcaps) then I want it to raise a custom error message and terminate the whole program while also not showing a traceback.

如果遇到错误,唯一应该打印的内容看起来像(我以3为例,格式化不是问题)。

If the error is encountered the only thing that should print out would look something like (I'm using 3 as an example, formatting is not a problem) this.

`Syntax Error (Line 3): No -THEN- present in the statement.`

我对于它实际上是一个异常类对象不是很挑剔,因此在这方面没有问题。由于我将在while循环中使用此代码,因此简单的if,elif会一遍又一遍地重复该消息(因为显然我没有关闭该循环)。我已经看过sys.exit()了,但是除非我使用不正确,否则它还会打印出一个巨大的红色文本块。我不想在循环中捕获异常,因为在同一模块中还有其他类需要在其中实现类似的东西。

I'm not very picky about it actually being an exception class object, so there's no issue in that aspect. Since I will be using this in a while loop, simple if, elif just repeats the message over and over (because obviously I am not closing the loop). I have seen sys.exit() but that also prints out a giant block of red text, unless I am not using it correctly. I don't want to catch the exception in my loop because there are other classes in the same module in which I need to implement something like this.

推荐答案

您可以使用 try:,然后使用,但inst例外:
这样做是在名为inst的变量中给您错误消息,然后您可以使用 inst.args 打印错误的参数。尝试将其打印出来,看看会发生什么,并且 inst.args 中的任何项目都是您要寻找的项目。

You can use a try: and then except Exception as inst: What that will do is give you your error message in a variable named inst and you can print out the arguments on the error with inst.args. Try printing it out and seeing what happens, and is any item in inst.args is the one you are looking for.

编辑这是我尝试使用python IDLE的示例:

EDIT Here is an example I tried with pythons IDLE:

>>> try:
    open("epik.sjj")
except Exception as inst:
    d = inst


>>> d
FileNotFoundError(2, 'No such file or directory')
>>> d.args
(2, 'No such file or directory')
>>> d.args[1]
'No such file or directory'
>>> 

编辑2:至于关闭程序,您总是可以提高和错误,或者您可以使用 sys.exit()

EDIT 2: as for closing the program you can always raise and error or you can use sys.exit()

这篇关于在不满足条件的情况下打印错误消息而不打印回溯并关闭程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆