Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to pass argument to an Exception in Python?

In the code below

try:
    b=float(56+78/0)
except Exception:
    print 'There is an error'

How to rewrite this code by passing an argument to the Exception?


1 Answer
Manogna

An exception can have an argument, which is a value that gives additional information about the problem. The contents of the argument vary from exception to exception. You capture an exception's argument by supplying a variable in the except clause as follows

try:
b=float(56+78/0)
except Exception, Argument:
print 'This is the Argument\n', Argument

The output obtained is as follows

This is the Argument
integer division or modulo by zero

If you write the code to handle a single exception, you can have a variable follow the name of the exception in the except statement. If you are trapping multiple exceptions, you can have a variable follow the tuple of the exception.

This variable receives the value of the exception mostly containing the cause of the exception. The variable can receive a single value or multiple values in the form of a tuple. This tuple usually contains the error string, the error number, and an error location.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.