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

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

问题描述

我见过与此类似的问题,但没有一个真正解决引用问题.如果我有这样的课

class Stop_if_no_then():def __init__(self, value one, operator, value_two, then, line_or_label, line_number):self._firstvalue = value_oneself._secondvalue = value_twoself._operator = 运算符self._gohere = line_or_labelself._then = 然后self._line_number = line_number定义执行(自我,其他类):代码比较前两个值并进行更改等"

我希望我的 execute 方法能够做的是,如果 self._then 不等于字符串THEN"(全大写),那么我希望它引发自定义错误消息并终止整个程序,同时也不显示回溯.

如果遇到错误,唯一应该打印出来的东西看起来像(我以 3 为例,格式不是问题)这个.

`语法错误(第 3 行):语句中不存在 -THEN-.`

我对它实际上是一个异常类对象并不是很挑剔,所以在这方面没有问题.因为我将在 while 循环中使用它,简单的 if, elif 只是一遍又一遍地重复消息(因为显然我没有关闭循环).我见过 sys.exit() 但它也会打印出一大块红色文本,除非我没有正确使用它.我不想在我的循环中捕获异常,因为在同一模块中还有其他类需要在其中实现类似的内容.

解决方案

您可以使用 try: 然后 except Exception as inst:这样做是在名为 inst 的变量中为您提供错误消息,您可以使用 inst.args 打印出错误的参数.试着打印出来看看会发生什么,inst.args 中的任何一项都是你要找的.

编辑这是我在 pythons IDLE 上尝试的一个例子:

<预><代码>>>>尝试:打开(epik.sjj")除了作为 inst 的异常:d = 指令>>>dFileNotFoundError(2, '没有那个文件或目录')>>>d.args(2, '没有那个文件或目录')>>>d.args[1]'无此文件或目录'>>>

EDIT 2:至于关闭程序,您可以随时raise 和错误,或者您可以使用 sys.exit()

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"

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.

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.`

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.

解决方案

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.

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'
>>> 

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

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

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