Prerequisite- This article is an extension to Exception Handling.
Python throws errors and exceptions, when there is a code gone wrong, which may cause program to stop abruptly. Python also provides exception handling method with the help of try-except. Some of the standard exceptions which are most frequent include IndexError, ImportError, IOError, ZeroDivisionError, TypeError and FileNotFoundError. A user can create his own error using exception class.
Creating User-defined Exception
Programmers may name their own exceptions by creating a new exception class. Exceptions need to be derived from the Exception class, either directly or indirectly. Although not mandatory, most of the exceptions are named as names that end in “Error” similar to naming of the standard exceptions in python. For example:
('A New Exception occured: ', 6)
Knowing all about Exception Class
To know more about about class Exception, run the code below
Deriving Error from Super Class Exception
Super class Exceptions are created when a module needs to handle several distinct errors. One of the common way of doing this is to create a base class for exceptions defined by that module. Further, various subclasses are defined to create specific exception classes for different error conditions.
('Exception occured: ', 'Not Allowed')
How to use standard Exceptions as base class?
Runtime error is a class is a standard exception which is raised when a generated error does not fall into any category. This program illustrates how to use runtime error as base class and network error as derived class. In a similar way, any exception can be derived from the standard exceptions of Python.
('E', 'r', 'r', 'o', 'r')
This article is contributed by Piyush Doorwar. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to email@example.com. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.
- Python | Catching and Creating Exceptions
- Exceptions - Selenium Python
- Errors and Exceptions in Python
- Built-in Exceptions in Python
- Exceptions in Java
- Important differences between Python 2.x and Python 3.x with examples
- OOP in Python | Set 3 (Inheritance, examples of object, issubclass and super)
- OS Module in Python with Examples
- Lambda and filter in Python Examples
- colorsys module in Python with examples
- Python datetime module with examples
- Python Logical Operators with Examples
- Copy a directory recursively using Python (with examples)
- Python - Lemmatization Approaches with Examples
- Regular Expression in Python with Examples | Set 1
- Timeit in Python with Examples
- Constructor Chaining In Java with Examples
- Types of Exception in Java with Examples
- C/C++ For loop with Examples
- Java For loop with Examples