0

I tried a syntax erroneous code in the try block, and tried to catch it. But I dont't know why It's keeping showing a syntax error.

def bad_fun(n):
    try:
        try:
            print("we're good!")
            print(int("7u"))
        except ValueError,ZeroDivisionError:
            print("Something wrong?")
        return 1 / n
    except ArithmeticError:
        print("Arithmetic Problem!")
    except SyntaxError:
        print("handled")
    return None

bad_fun(0)

print("THE END.")

The syntax error passed unhandled.

kal
  • 1
  • 1
  • 3
    You can't trap syntax errors - by definition the interpreter is not able to parse code with syntax errors. – snakecharmerb Dec 01 '22 at 09:03
  • 1
    try-catch is used for run-time errors whereas syntax errors are compile-time errors and hence will throw an error when the code is compiled. – em_bis_me Dec 01 '22 at 09:27

0 Answers0