简体   繁体   English

删除默认异常消息并仅在python中打印自定义消息

[英]Remove default exception message and print only custom message in python

Python 2.7 : I am trying to create method which check whether input time is according to format as YYYY-MM-DD . Python 2.7 :我正在尝试创建一种方法,该方法检查输入时间是否符合YYYY-MM-DD格式。 If the date is not as per given format, send user custom message that "Please change the format as YYYY-MM-DD" . 如果日期不符合给定格式,请发送用户自定义消息“请将该格式更改为YYYY-MM-DD” If the date is as per format please check whether it is greater than today's date, if not again send user custom message. 如果日期符合格式,请检查日期是否大于今天的日期,否则请再次发送用户自定义消息。 In all the cases I just need to come out of the method sending custom message and not to break the program. 在所有情况下,我只需要退出发送自定义消息的方法,而不必破坏程序。

def validate_timeline(time):
    try:
        datetime.datetime.strptime(time, '%Y-%m-%d')
        try:
            time = datetime.datetime.strptime(time, '%Y-%m-%d')
            dat = datetime.datetime.today()
            if dat > time:
                print("Please enter the future date")
                raise Exception("Choose the date greater than today's date")
        except ValueError:
            raise ValueError("Incorrect date format, should be YYYY-MM-DD")

    except ValueError:
        raise ValueError("Incorrect date format, should be YYYY-MM-DD")

I am still getting default exception message with custom message. 我仍然收到带有自定义消息的默认异常消息。

Exception !! Incorrect date format, should be YYYY-MM-DD Traceback :
Traceback (most recent call last):
  File "My_file.py", line 511, in <module>
    validate_timeline(time)
  File "My_file.py", line 62, in validate_timeline
    raise ValueError("Incorrect date format, should be YYYY-MM-DD")
ValueError: Incorrect date format, should be YYYY-MM-DD

Desired output: 所需的输出:

Enter the command: set timeout 2017-09- to file
"Incorrect date format, should be YYYY-MM-DD"
Enter the command:

Here the program should ask to enter the command(raw_input) again and should not come out of the program. 此处程序应要求再次输入命令(raw_input),并且不应退出程序。

code: 码:

 elif(condition):
            lis = opt.split()
            timeline = lis[2]
            user = lis[4]
            grp = lis[7]
            validate_timeline(timeline)
            with open("test.txt","r") as f:

The way you raise the exception seems fine. raise异常的方式似乎很好。 The problem is probably on the calling side. 问题可能出在调用方。 If you do not want those stack traces to print and the program to crash, you have to handle the exception yourself and just print it. 如果您不希望打印这些堆栈跟踪信息并使程序崩溃,则必须自己处理该异常并仅打印它。

cmd = input("Enter the command:")
try:
    validate_timeline(cmd)
    # do other things
except ValueError as e:
    print(e)

Example output: 输出示例:

Enter the command: not a real date
Incorrect date format, should be YYYY-MM-DD

Don't raise those specific exceptions, instead write your custom message to continue with the program execution 不要提出这些特定的例外,而是编写您的自定义消息以继续执行程序

def validate_timeline(time):
    try:
        datetime.datetime.strptime(time, '%Y-%m-%d')
        try:
            time = datetime.datetime.strptime(time, '%Y-%m-%d')
            dat = datetime.datetime.today()
            if dat > time:
                print("Please enter the future date")
                #raise Exception("Choose the date greater than today's date")
        except ValueError:
            #raise ValueError("Incorrect date format, should be YYYY-MM-DD")
            print("Incorrect date format, should be YYYY-MM-DD")

    except ValueError:
        #raise ValueError("Incorrect date format, should be YYYY-MM-DD")
        print("Incorrect date format, should be YYYY-MM-DD")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM