- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
User-Defined Exceptions in Python
Python also allows you to create your own exceptions by deriving classes from the standard built-in exceptions.
Here is an example related to RuntimeError. Here, a class is created that is subclassed from RuntimeError. This is useful when you need to display more specific information when an exception is caught.
In the try block, the user-defined exception is raised and caught in the except block. The variable e is used to create an instance of the class Networkerror.
class Networkerror(RuntimeError): def __init__(self, arg): self.args = arg
So once you defined above class, you can raise the exception as follows −
try: raise Networkerror("Bad hostname") except Networkerror,e: print e.args
- User-defined Exceptions in Python with Examples
- What are user-defined exceptions in C#?
- User-defined Exceptions in C# with Example
- User defined and custom exceptions in Java
- How to create user defined exceptions in C#?
- PHP User-defined functions
- How to implement user defined exception in Python?
- User Defined Literals in C++
- User-defined Custom Exception in C#
- Using User-Defined Variables in MySQL
- What is user-defined signal handler?
- User defined bridge on Docker network
- Perform MySQL SELECT INTO user-defined variable
- C++ set for user defined data type?
- What are user defined data types in C#?