In response to being able to override assertion error, someone suggested I do this.
Can anyone help me on how I can override the AssertionError class so that I can do an AssertionError and it will call my custom one such as the one in the picture?
In response to being able to override assertion error, someone suggested I do this.
Can anyone help me on how I can override the AssertionError class so that I can do an AssertionError and it will call my custom one such as the one in the picture?
You can't override default exceptions but you can create a new exception class with the same name and import it in the file where you want to use it as shown in the image you have shared.
For example:
custom_exception.py
class CustomAssertionError(Exception):
# Constructor or Initializer
def __init__(self, message):
# do your stuff here
raise AssertionError(message)
usage.py
from custom_exception import AssertionError
def my_function():
raise CustomAssertionError("some error message")
For more details about creating custom exceptions in python please follow: Proper way to declare custom exceptions in modern Python?
Here is a way to do this. It is ugly, and probably not very much recommended, but it works: (Note, this works in Python3, Python2.7 is different)