Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Python, what's the difference between 'except Exception as e' and 'except Exception, e' [duplicate]

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?

like image 923
Nathan Avatar asked Feb 25 '11 16:02

Nathan


People also ask

What's the difference between except exception E and except exception as e?

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.

What is the difference between Except and except exception in Python?

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:).

What is exception as e in Python?

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.

What is except as Python?

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.


1 Answers

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"

like image 85
Ignacio Vazquez-Abrams Avatar answered Oct 13 '22 13:10

Ignacio Vazquez-Abrams