Argument of an Exception in Python

PythonServer Side ProgrammingProgramming

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

try:
   You do your operations here;
   ......................
except ExceptionType, Argument:
   You can print value of Argument here...

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.

Example

Following is an example for a single exception −

 Live Demo

#!/usr/bin/python
# Define a function here.
def temp_convert(var):
   try:
      return int(var)
   except ValueError, Argument:
      print "The argument does not contain numbers\n", Argument
# Call above function here.
temp_convert("xyz");

Output

This produces the following result −

The argument does not contain numbers
invalid literal for int() with base 10: 'xyz'
raja
Published on 30-Jan-2020 06:59:44
Advertisements