在Python中,如果在不符合条件的情况下,如何打印错误消息而不打印回溯并关闭程序? [英] In Python, how do I print an error message without printing a traceback and close the program when a condition is not met?

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

问题描述

我已经看到类似的问题,但没有一个真正解决这个问题。
如果我有这样的类

  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):
代码比较前两个值并进行更改

我希望我的执行方法能够做的是如果self._then不等于字符串THEN(在allcaps中)然后我希望它提出一个自定义的错误消息并终止整个程序,同时也没有显示回溯。



如果遇到错误应该打印出来的东西看起来像(我使用3作为示例,格式化不是问题)。

 `语法错误(第3行):没有-THEN-存在于语句中。

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

解决方案

您可以使用 try:然后,除了例外为inst:
什么这将是一个名为inst的变量的错误消息,您可以使用 inst.args 打印错误的参数。尝试打印出来,看看会发生什么,并且 inst.args 中的任何项目是您要查找的项目。



 >>>尝试:
open(epik.sjj)
除了异常作为inst:
d = inst


>>> d
FileNotFoundError(2,'没有这样的文件或目录')
>>> d.args
(2,没有这样的文件或目录)
>>> d.args [1]
'没有这样的文件或目录'
>>>

编辑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()

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

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