
- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
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
How to pass a variable to an exception in Python?
Here we are passing a variable to given exception. We are defining a custom exception ExampleException which is a subclass of base class Exception and also defining the __init__ method. We use a try-except block to raise the exception and pass the variable to the exception as follows.
Example
class ExampleException(Exception): def __init__(self, foo): self.foo = foo try: raise ExampleException("Bar!") except ExampleException as e: print e.foo
Output
"C:/Users/TutorialsPoint1/~bar.py" Bar!
- Related Questions & Answers
- How to pass argument to an Exception in Python?
- What is the correct way to pass an object with a custom exception in Python?
- How to handle an exception in Python?
- How to raise an exception in Python?
- How to pass a variable from Activity to Fragment in Android?
- How do I pass a variable to a MySQL script?
- How to pass variable in one Python CGI script to other Python CGI script?
- How to pass a variable from BeforeTest to Test annotation in TestNG?
- How to pass a date variable in sql query in a JSP?
- How to pass a variable into a jQuery attribute-contains selector?
- How to ignore an exception and proceed in Python?
- How to capture an exception raised by a Python Regular expression?
- How to handle an exception in JSP?
- How to rethrow an exception in Java?
- How to pass optional parameters to a function in Python?
Advertisements