cyborg
cyborg

Reputation: 878

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. If the date is not as per given format, send user custom message that "Please change the format as 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.

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:

Upvotes: 1

Views: 1036

Answers (2)

tobias_k
tobias_k

Reputation: 82929

The way you raise the exception seems fine. 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

Upvotes: 1

Van Peer
Van Peer

Reputation: 2167

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")

Upvotes: 0

Related Questions