
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Argument of an Exception in Python
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 −
#!/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'
- Related Articles
- How to pass argument to an Exception in Python?
- How to handle an exception in Python?
- How to raise an exception in Python?
- Argument Parsing in Python
- How to ignore an exception and proceed in Python?
- How will you explain that an exception is an object in Python?
- How to pass a variable to an exception in Python?
- How do I manually throw/raise an exception in Python?
- How do you handle an exception thrown by an except clause in Python?
- What is Exception in Python?
- Return the angle of the complex argument in Python
- What is exception handling in Python?
- Change the real part of the complex argument in Python
- Return the imaginary part of the complex argument in Python
- Change the imaginary part of the complex argument in Python
