Having to handle exceptions is common in Python and so is having to define your own. Yet, I have seen competing ways of doing so in various projects. The inconsistency comes from Exceptions being something that can easily be subclassed and extended, but also something that can be easily instantiated and used in their base form.

Here is a common way to define custom exceptions in Python:

class MyException(Exception): def __init__(self, msg): self.msg = msg try: raise 
MyException("Something went wrong") except MyException as e: print(e) # <<< Something went wrong print(repr(e)) # <<< MyException('Something went wrong')

#python-programming #learn-python #python-tips #python-developers #python #coding

Consistent Custom Exception Classes in Python
2.95 GEEK