
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Pass a Variable to an Exception in Python
To pass a variable to an exception in Python, provide the variable as an argument when raising the exception. For custom exceptions, store the variable in an attribute.
You can pass variables like strings or numbers directly into built-in exceptions to include dynamic data in the error message.
Example: Passing a variable to a ValueError
In this example, we are passing a variable containing an invalid input message to a ValueError -
value = "abc123" try: raise ValueError(f"Invalid input: {value}") except ValueError as e: print("Caught exception:", e)
We get the following output -
Caught exception: Invalid input: abc123
Passing Arguments to an Exception
You can create your own exception class and print values (such as strings, numbers, lists, or even custom objects) or, your own exception message by passing them as a parameter while raising the exception explicitly. The values/variable can be stored as an instance attribute for later access.
Example: Custom Exception with a Variable
In this example, we are passing an error message stored in a variable to a custom exception -
class MyError(Exception): def __init__(self, message): self.message = message super().__init__(message) msg = "Something went wrong." try: raise MyError(msg) except MyError as e: print("Caught custom exception:", e.message)
Following is the output obtained -
Caught custom exception: Something went wrong.
Passing Multiple Variables
You can also pass multiple variables to an exception and use them to generate a more descriptive error message.
Example
In this example, we are passing a variable for the username and the error reason to a custom exception -
class LoginError(Exception): def __init__(self, user, reason): self.user = user self.reason = reason super().__init__(f"User '{user}' failed to login: {reason}") username = "suresh_raina" reason = "Invalid password" try: raise LoginError(username, reason) except LoginError as e: print("Caught exception:", e)
We get the output as shown below -
Caught exception: User 'suresh_raina' failed to login: Invalid password
Accessing the Variable Later
If you store the passed variable in an instance variable inside the custom exception class, you can access it even after catching the exception.
Example
In this example, we are accessing the variable stored in the exception object after catching it -
class DataMissingError(Exception): def __init__(self, missing_field): self.missing_field = missing_field super().__init__(f"Missing data: {missing_field}") try: field = "email" raise DataMissingError(field) except DataMissingError as e: print("Missing field:", e.missing_field)
We get the following output -
Missing field: email