Please note, this is a STATIC archive of website www.tutorialspoint.com from 11 May 2019, cach3.com does not collect or store any user information, there is no "phishing" involved.
Tutorialspoint

How to declare custom exceptions in modern Python?

To declare a simple custom exception we can have a script like this

class MyException(Exception):
    pass

But how do I declare a custom exception to override something or to pass extra arguments to the exception?


1 Answer
Rajendra Dharmkar

To override something or pass extra arguments to the exception we do like this in modern python:

class ValidationError(Exception):
def __init__(self, message, errors):
super(ValidationError, self).__init__(message)
self.errors = errors

That way we could pass dictionary of error messages to the second parameter, and get to it later on when needed.

Advertisements

We use cookies to provide and improve our services. By using our site, you consent to our Cookies Policy.