2
\$\begingroup\$

I got this program that I am working on and in it, I got a section that has a try statement and two error except statements.

try:
    ...
    if ...: raise SyntaxError
except SyntaxError:
    ...
except ValueError:
    ...

The thing is that the SyntaxErrors are technically ValueErrors, but the errors occur for different reasons, therefore need different code to solve/get around the problem. So in order to prevent the code in all the "excepts" from being executed, I had to separate/make the type of Errors different. Is it okay to do that even though the SyntaxErrors are techincally ValueErrors, that I label them as SyntaxErrors? Or is there a method in solving this problem?

Here is the code of the program:

from datetime import datetime

today = datetime(2019, 1, 1).date() 
this_year = 2019
last_year = this_year - 1

text = ["Dec 31 ", "@@@@@@@@@@@@@@@@@@@@@Dec 31 "]

for line in text:
    date_str = line
    try:
        date = datetime.strptime(date_str + str(this_year), "%b %d %Y").date()
        if date > today: raise SyntaxError
        print("+", date)
    except SyntaxError:
        date = datetime.strptime(date_str + str(last_year), "%b %d %Y").date()
        print("-", date)
    except ValueError:
        print("abnormality")
        continue
\$\endgroup\$
3
\$\begingroup\$

Obtaining a date that is in the future is in no way a "syntax error", and it would be inappropriate to raise a SyntaxError. Instead of abusing the exception-handling mechanism, you should just use a conditional.

for date_str in text:
    try:
        date = datetime.strptime(date_str + str(this_year), "%b %d %Y").date()
        if date <= today:
            print("+", date)
        else:
            date = datetime.strptime(date_str + str(last_year), "%b %d %Y").date()
            print("-", date)
    except ValueError:
        print("abnormality")

Note that you might be misinterpreting some ValueErrors as abnormalities. For example, "Feb 29 2021" would cause a ValueError, but then you would never get a chance to try "Feb 29 2020", which is a valid day in a leap year.

\$\endgroup\$
  • \$\begingroup\$ Ok thank you. But if the current date is Feb 29 2021, there would be no point in look at Feb 29 2020 since it is a year behind. \$\endgroup\$ – StrangeRanger Aug 14 '18 at 6:14
  • \$\begingroup\$ I think you thought that the program is supposed to look into future dates/year. But it is not supposed to do that. It looks at the past, in a sense. It moves the year backwards, but keeps the months and days the same. This is to prevent the variable “date” from equaling “Dec 31 2020” if Ithe current date is “Jan 1 2020”. The reason for that dives into a much bigger program. The current program that I posted was a small snippet that could work independently from the actual program it was apart of. \$\endgroup\$ – StrangeRanger Aug 14 '18 at 6:22
  • \$\begingroup\$ Your goal may be to ensure that date is in the past. However, your implementation works by initially attempting to parse the date in the this_year calendar year, which may trigger an unintended ValueError before you even get a chance to do the if date > today test. \$\endgroup\$ – 200_success Aug 14 '18 at 6:25
  • \$\begingroup\$ But a ValueError shouldn’t even come up because if I am looking at this years calendar and if there a a valid leap year, or if there is not, it will follow the rule of the calendar. \$\endgroup\$ – StrangeRanger Aug 14 '18 at 6:28
  • \$\begingroup\$ It shouldn’t say Feb 29 if on this years calendar there is no Feb 29 since the program goes by this years calendar. \$\endgroup\$ – StrangeRanger Aug 14 '18 at 6:29
0
\$\begingroup\$

just a note on syntax error. a syntax error occurs when you have a fault with your writing of python, not with your logic or values. if the lexer goes through your program and finds if x == 3 ::, it will raise a syntax error as it finds an unexpected :. but 3 + 'm' is not of this type as the lexer finds nothing wrong in it.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.