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