In python, there are two ways to catch an exception
except Exception, e: except Exception as e:
It seems like 'as e' is the one to use going forward. In what version of python did this change? Any idea why?
The simple except statement is utilized in general cases, and it excepts all the exceptions. In contrast, the except Exception as e statement is a statement that defines an argument to the except statement.
A bare except: clause will catch SystemExit and KeyboardInterrupt exceptions, making it harder to interrupt a program with Control-C, and can disguise other problems. If you want to catch all exceptions that signal program errors, use except Exception: (bare except is equivalent to except BaseException:).
An exception is an event, which occurs during the execution of a program that disrupts the normal flow of the program's instructions. In general, when a Python script encounters a situation that it cannot cope with, it raises an exception. An exception is a Python object that represents an error.
The try block lets you test a block of code for errors. The except block lets you handle the error. The else block lets you execute code when there is no error.
This PEP introduces changes intended to help eliminate ambiguities in Python's grammar, simplify exception classes, simplify garbage collection for exceptions and reduce the size of the language in Python 3.0.
PEP 3110: "Catching Exceptions in Python 3000"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With