How to declare custom exceptions in modern Python?


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.

Updated on: 26-Sep-2019

137 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements