Reputation: 29706
What's the proper way to declare custom exception classes in modern Python? My primary goal is to follow whatever standard other exception classes have, so that (for instance) any extra string I include in the exception is printed out by whatever tool caught the exception.
By "modern Python" I mean something that will run in Python 2.5 but be 'correct' for the Python 2.6 and Python 3.* way of doing things. And by "custom" I mean an Exception
object that can include extra data about the cause of the error: a string, maybe also some other arbitrary object relevant to the exception.
I was tripped up by the following deprecation warning in Python 2.6.2:
>>> class MyError(Exception):
... def __init__(self, message):
... self.message = message
...
>>> MyError("foo")
_sandbox.py:3: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
It seems crazy that BaseException
has a special meaning for attributes named message
. I gather from PEP-352 that attribute did have a special meaning in 2.5 they're trying to deprecate away, so I guess that name (and that one alone) is now forbidden? Ugh.
I'm also fuzzily aware that Exception
has some magic parameter args
, but I've never known how to use it. Nor am I sure it's the right way to do things going forward; a lot of the discussion I found online suggested they were trying to do away with args in Python 3.
Update: two answers have suggested overriding __init__
, and __str__
/__unicode__
/__repr__
. That seems like a lot of typing, is it necessary?
Upvotes: 1942
Views: 1289580
Reputation: 394775
"What is the proper way to declare custom exceptions in modern Python?"
This is fine unless your exception is really a type of a more specific exception:
class MyException(Exception):
pass
Or better (maybe perfect), instead of pass
give a docstring:
class MyException(Exception):
"""Raise for my specific kind of exception"""
From the docs
Exception
All built-in, non-system-exiting exceptions are derived from this class. All user-defined exceptions should also be derived from this class.
That means that if your exception is a type of a more specific exception, subclass that exception instead of the generic Exception
(and the result will be that you still derive from Exception
as the docs recommend). Also, you can at least provide a docstring (and not be forced to use the pass
keyword):
class MyAppValueError(ValueError):
'''Raise when my specific value is wrong'''
Set attributes you create yourself with a custom __init__
. Avoid passing a dict as a positional argument, future users of your code will thank you. If you use the deprecated message attribute, assigning it yourself will avoid a DeprecationWarning
:
class MyAppValueError(ValueError):
'''Raise when a specific subset of values in context of app is wrong'''
def __init__(self, message, foo, *args):
self.message = message # without this you may get DeprecationWarning
# Special attribute you desire with your Error,
# perhaps the value that caused the error?:
self.foo = foo
# allow users initialize misc. arguments as any other builtin Error
super(MyAppValueError, self).__init__(message, foo, *args)
There's really no need to write your own __str__
or __repr__
. The built-in ones are very nice, and your cooperative inheritance ensures that you use them.
Maybe I missed the question, but why not:
class MyException(Exception): pass
Again, the problem with the above is that in order to catch it, you'll either have to name it specifically (importing it if created elsewhere) or catch Exception, (but you're probably not prepared to handle all types of Exceptions, and you should only catch exceptions you are prepared to handle). Similar criticism to the below, but additionally that's not the way to initialize via super
, and you'll get a DeprecationWarning
if you access the message attribute:
Edit: to override something (or pass extra args), do this:
class ValidationError(Exception): def __init__(self, message, errors): # Call the base class constructor with the parameters it needs super(ValidationError, self).__init__(message) # Now for your custom code... self.errors = errors
That way you could pass dict of error messages to the second param, and get to it later with
e.errors
.
It also requires exactly two arguments to be passed in (aside from the self
.) No more, no less. That's an interesting constraint that future users may not appreciate.
To be direct - it violates Liskov substitutability.
I'll demonstrate both errors:
>>> ValidationError('foo', 'bar', 'baz').message
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
ValidationError('foo', 'bar', 'baz').message
TypeError: __init__() takes exactly 3 arguments (4 given)
>>> ValidationError('foo', 'bar').message
__main__:1: DeprecationWarning: BaseException.message has been deprecated as of Python 2.6
'foo'
Compared to:
>>> MyAppValueError('foo', 'FOO', 'bar').message
'foo'
Upvotes: 327
Reputation: 4160
There are good points in several answers but I'm still working on getting a clear understanding of what the Python recommended approach to customised exceptions is in v3.13. I also feel the other answers mostly address aspects of the approach without a full description of implementation and usage.
At the risk of crowding this question I'm putting my current approach (using Python v3.10) with a usage scenario to attempt to collate the most recent advice.
This example has characteristics that suit a REST API implementation but can be repurposed.
The recommended principles as far as I can determine are:
Exception
super().__init__
with one str
argument__str__
to control display of the exceptionsThis example does not handle additional parameters to the child exceptions. Sorry for this omission. The example does support additional class attributes which provide detail without needing to pass parameters.
In usage I sometimes convert built-in exceptions to custom exceptions for uniformity of processing.
exceptions.py:
"""Exceptions identify application status and primary error message.
Exceptions are used to abort processing
and return execution to the handler whenever processing is blocked.
An Exception is also returned when processing is completed
to indicate HTTP 200 status to caller with or without a raise.
- https://www.iana.org/assignments/http-status-codes/http-status-codes.xhtml
- https://developer.mozilla.org/en-US/docs/Web/HTTP/Status
- https://docs.python.org/3/library/http.html#http.HTTPStatus
"""
import http
import sys
import traceback
class APIException(Exception):
"""Response message and HTTP status. Always subclass, do not use in code."""
message: str = http.HTTPStatus.IM_A_TEAPOT.phrase # One standard Exception argument
code: int = http.HTTPStatus.IM_A_TEAPOT.value # code is not a parameter
def __init__(self, message: str = "") -> None:
"""Provide reliably located customisable message."""
self.message = message or self.message
super().__init__(self.message)
def __str__(self) -> str:
return str(self.code) + ": " + self.message + ": " + self.location(self)
@staticmethod # Can pass Exception that are not APIException
def location(e: Exception | None = None) -> str:
"""Return string with ``traceback`` module and line number e.g. ``"init#67"``."""
tb = e and e.__traceback__ or sys.exc_info()[-1] # Use provided or current Exception
if not tb: # Exception instances that are not raised do not have a traceback
return ""
frame = traceback.extract_tb(tb)[-1]
return frame[2] + "#" + str(frame[1])
class ServerError(APIException):
code = http.HTTPStatus.INTERNAL_SERVER_ERROR.value # HTTP 500
message = "Server error"
class NotImplemented(APIException): # noqa: A001: not shadowing builtin
code = http.HTTPStatus.NOT_IMPLEMENTED.value # HTTP 501
message = "Not Implemented"
class BadRequest(APIException):
code = http.HTTPStatus.BAD_REQUEST.value # HTTP 400
message = "Invalid request or parameters"
class NotFound(BadRequest):
code = http.HTTPStatus.NOT_FOUND.value # HTTP 404
message = "Requested calculation not found"
class OK(APIException):
code = http.HTTPStatus.OK.value # HTTP 200
message = http.HTTPStatus.OK.phrase
# vim: set list colorcolumn=121:
To illustrate some common usage, wrapping application code in a try
block and handling a variety of errors from different sources, this example has a single catch sequence and all pathways always exit with a defining APIException
instance referred to by the status
variable. This can also be exceptions.OK
for the success pathway. status
then always contains status.code
with an int
status code and status.message
for the response description, which will mostly be "OK"
for successful responses.
handler.py:
try:
...
except json.JSONDecodeError as e:
status = exceptions.BadRequest("JSONDecodeError: " + str(e))
except pydantic.ValidationError as e:
errors = e.errors()[0]
msg = ": ".join((str(errors["msg"]), str(errors["loc"]), str(errors["type"])))
msg = "ValidationError: {}".format(msg)
status = exceptions.BadRequest(msg)
except exceptions.APIException as e:
status = e
status.message = type(e).__name__ + ": " + e.message
except Exception as e: # noqa: BLE001 do catch all Exception in this case
traceback.print_exc() # Allow tracebacks to console
msg = "Unexpected {}: {}: {}"
msg = msg.format(type(e).__name__, str(e), exceptions.APIException.location(e))
status = exceptions.ServerError(msg)
...
Notice how the APIException
location
static method can be used from the class to return a uniform string from other exceptions:
exceptions.APIException.location(<any Exception>)
Upvotes: -1
Reputation: 303
Agreed with @sultanorazbayev's answer. Should use dataclasses for simplicity
import logging
from dataclasses import dataclass
logging.basicConfig(level=logging.DEBUG)
logger = logging.getLogger(__name__)
def main():
@dataclass
class MyCustomException(Exception):
code: int
message: str
description: str
try:
raise MyCustomException(
404,
"Resource not found",
"This resource does not exist or unavailable"
)
except MyCustomException as e:
logger.info(
f"""
MyCustomException caught:
{e.code}
{e.message}
{e.description}
"""
)
finally:
logger.info("Cleaning up and exiting...")
if __name__ == "__main__":
main()
Upvotes: -1
Reputation: 1843
See a very good article "The definitive guide to Python exceptions". The basic principles are:
BaseException.__init__
with only one argument.There is also information on organizing (in modules) and wrapping exceptions, I recommend to read the guide.
Upvotes: 17
Reputation: 16551
It's possible to use dataclass
to simplify the definition of the custom exception:
from dataclasses import dataclass
@dataclass
class MyException(Exception):
message: str = "This is a custom exception"
def __str__(self):
return f"Custom message: {self.message.upper()}"
raise MyException("abcdef")
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# MyException: Custom message: ABCDEF
raise MyException()
# Traceback (most recent call last):
# File "<stdin>", line 1, in <module>
# MyException: Custom message: THIS IS A CUSTOM EXCEPTION
This reduces some of the boilerplate, while remaining flexible for further customization.
Upvotes: 5
Reputation: 149726
To define your own exceptions correctly, there are a few best practices that you should follow:
Define a base class inheriting from Exception
. This will allow to easily catch any exceptions related to the project:
class MyProjectError(Exception):
"""A base class for MyProject exceptions."""
Organizing the exception classes in a separate module (e.g. exceptions.py
) is generally a good idea.
To create a specific exception, subclass the base exception class.
class CustomError(MyProjectError):
"""A custom exception class for MyProject."""
You can subclass custom exception classes as well to create a hierarchy.
To add support for extra argument(s) to a custom exception, define an __init__()
method with a variable number of arguments. Call the base class's __init__()
, passing any positional arguments to it (remember that BaseException
/Exception
expect any number of positional arguments). Store extra keyword arguments to the instance, e.g.:
class CustomError(MyProjectError):
def __init__(self, *args, **kwargs):
super().__init__(*args)
self.custom_kwarg = kwargs.get('custom_kwarg')
Usage example:
try:
raise CustomError('Something bad happened', custom_kwarg='value')
except CustomError as exc:
print(f'Сaught CustomError exception with custom_kwarg={exc.custom_kwarg}')
This design adheres to the Liskov substitution principle, since you can replace an instance of a base exception class with an instance of a derived exception class. Also, it allows you to create an instance of a derived class with the same parameters as the parent.
Upvotes: 101
Reputation: 3273
For maximum customisation, to define custom errors, you may want to define an intermediate class that inherits from Exception
class as:
class BaseCustomException(Exception):
def __init__(self, msg):
self.msg = msg
def __repr__(self):
return self.msg
class MyCustomError(BaseCustomException):
"""raise my custom error"""
Upvotes: 10
Reputation: 766
For me it is just __init__
and variables but making sometimes testing.
My sample:
Error_codes = { 100: "Not enough parameters", 101: "Number of special characters more than limits", 102: "At least 18 alphanumeric characters and list of special chars !@#$&*" }
class localbreak( Exception ) :
Message = ""
def __init__(self, Message):
self.Message = Message
return
def __str__(self):
print(self.Message)
return "False"
### When calling ...
raise localbreak(Error_codes[102])
Output:
Traceback (most recent call last): File "ASCII.py", line 150, in <module>
main(OldPassword, Newpassword) File "ASCII.py", line 39, in main
result = read_input("1", "2", Newpassword, "4")
File "ASCII.py", line 69, in read_input
raise localbreak(Error_codes[102]) At least 18 alphanumeric characters and list of special chars !@#$&*
__main__.localbreak: False
Upvotes: -3
Reputation: 326
I came across this thread. This is how I do custom exceptions. While the Fault
class is slightly complex, it makes declaring custom expressive exceptions with variable arguments trivial.
FinalViolation
, SingletonViolation
are both sub classes of TypeError
so will be caught code below.
try:
<do something>
except TypeError as ex:
<handler>
That's why Fault
doesn't inherit from Exception
. To allow derivative exceptions to inherit from the exception of their choice.
class Fault:
"""Generic Exception base class. Note not descendant of Exception
Inheriting exceptions override formats"""
formats = '' # to be overriden in descendant classes
def __init__(self, *args):
"""Just save args for __str__"""
self.args = args
def __str__(self):
"""Use formats declared in descendant classes, and saved args to build exception text"""
return self.formats.format(*self.args)
class TypeFault(Fault, TypeError):
"""Helper class mixing Fault and TypeError"""
class FinalViolation(TypeFault):
"""Custom exception raised if inheriting from 'final' class"""
formats = "type {} is not an acceptable base type. It cannot be inherited from."
class SingletonViolation(TypeFault):
"""Custom exception raised if instancing 'singleton' class a second time"""
formats = "type {} is a singleton. It can only be instanced once."
FinalViolation
, SingletonViolation
unfortunately only accept 1 argument.
But one could easily create a multi arg error e.g.
class VesselLoadingError(Fault, BufferError):
formats = "My {} is full of {}."
raise VesselLoadingError('hovercraft', 'eels')
__main__.VesselLoadingError: My hovercraft is full of eels.
Upvotes: 0
Reputation: 172181
No, "message" is not forbidden. It's just deprecated. You application will work fine with using message. But you may want to get rid of the deprecation error, of course.
When you create custom Exception classes for your application, many of them do not subclass just from Exception, but from others, like ValueError
or similar. Then you have to adapt to their usage of variables.
And if you have many exceptions in your application it's usually a good idea to have a common custom base class for all of them, so that users of your modules can do
try:
...
except NelsonsExceptions:
...
And in that case you can do __init__
and __str__
needed there, so you don't have to repeat it for every exception. But simply calling the message variable something else than message does the trick.
In any case, you only need __init__
or __str__
if you do something different from what Exception itself does. And because if the deprecation, you then need both, or you get an error. That's not a whole lot of extra code you need per class.
Upvotes: 10
Reputation: 322
I had issues with the above methods, as of Python 3.9.5. However, I found that this works for me:
class MyException(Exception):
"""Port Exception"""
And then it could be used in code like:
try:
raise MyException('Message')
except MyException as err:
print (err)
Upvotes: 8
Reputation: 137252
Maybe I missed the question, but why not:
class MyException(Exception):
pass
To override something (or pass extra args), do this:
class ValidationError(Exception):
def __init__(self, message, errors):
# Call the base class constructor with the parameters it needs
super().__init__(message)
# Now for your custom code...
self.errors = errors
That way you could pass dict of error messages to the second param, and get to it later with e.errors
.
In Python 2, you have to use this slightly more complex form of super()
:
super(ValidationError, self).__init__(message)
Upvotes: 2028
Reputation: 405
A really simple approach:
class CustomError(Exception):
pass
raise CustomError("Hmm, seems like this was custom coded...")
Or, have the error raise without printing __main__
(may look cleaner and neater):
class CustomError(Exception):
__module__ = Exception.__module__
raise CustomError("Improved CustomError!")
Upvotes: 4
Reputation: 3869
As of Python 3.8 (2018, https://docs.python.org/dev/whatsnew/3.8.html), the recommended method is still:
class CustomExceptionName(Exception):
"""Exception raised when very uncommon things happen"""
pass
Please don't forget to document, why a custom exception is neccessary!
If you need to, this is the way to go for exceptions with more data:
class CustomExceptionName(Exception):
"""Still an exception raised when uncommon things happen"""
def __init__(self, message, payload=None):
self.message = message
self.payload = payload # you could add more args
def __str__(self):
return str(self.message) # __str__() obviously expects a string to be returned, so make sure not to send any other data types
and fetch them like:
try:
raise CustomExceptionName("Very bad mistake.", "Forgot upgrading from Python 1")
except CustomExceptionName as error:
print(str(error)) # Very bad mistake
print("Detail: {}".format(error.payload)) # Detail: Forgot upgrading from Python 1
payload=None
is important to make it pickle-able. Before dumping it, you have to call error.__reduce__()
. Loading will work as expected.
You maybe should investigate in finding a solution using pythons return
statement if you need much data to be transferred to some outer structure. This seems to be clearer/more pythonic to me. Advanced exceptions are heavily used in Java, which can sometimes be annoying, when using a framework and having to catch all possible errors.
Upvotes: 70
Reputation: 2777
Try this Example
class InvalidInputError(Exception):
def __init__(self, msg):
self.msg = msg
def __str__(self):
return repr(self.msg)
inp = int(input("Enter a number between 1 to 10:"))
try:
if type(inp) != int or inp not in list(range(1,11)):
raise InvalidInputError
except InvalidInputError:
print("Invalid input entered")
Upvotes: 3
Reputation: 7635
With modern Python Exceptions, you don't need to abuse .message
, or override .__str__()
or .__repr__()
or any of it. If all you want is an informative message when your exception is raised, do this:
class MyException(Exception):
pass
raise MyException("My hovercraft is full of eels")
That will give a traceback ending with MyException: My hovercraft is full of eels
.
If you want more flexibility from the exception, you could pass a dictionary as the argument:
raise MyException({"message":"My hovercraft is full of animals", "animal":"eels"})
However, to get at those details in an except
block is a bit more complicated. The details are stored in the args
attribute, which is a list. You would need to do something like this:
try:
raise MyException({"message":"My hovercraft is full of animals", "animal":"eels"})
except MyException as e:
details = e.args[0]
print(details["animal"])
It is still possible to pass in multiple items to the exception and access them via tuple indexes, but this is highly discouraged (and was even intended for deprecation a while back). If you do need more than a single piece of information and the above method is not sufficient for you, then you should subclass Exception
as described in the tutorial.
class MyError(Exception):
def __init__(self, message, animal):
self.message = message
self.animal = animal
def __str__(self):
return self.message
Upvotes: 744
Reputation: 2272
You should override __repr__
or __unicode__
methods instead of using message, the args you provide when you construct the exception will be in the args
attribute of the exception object.
Upvotes: 19
Reputation: 19895
see how exceptions work by default if one vs more attributes are used (tracebacks omitted):
>>> raise Exception('bad thing happened')
Exception: bad thing happened
>>> raise Exception('bad thing happened', 'code is broken')
Exception: ('bad thing happened', 'code is broken')
so you might want to have a sort of "exception template", working as an exception itself, in a compatible way:
>>> nastyerr = NastyError('bad thing happened')
>>> raise nastyerr
NastyError: bad thing happened
>>> raise nastyerr()
NastyError: bad thing happened
>>> raise nastyerr('code is broken')
NastyError: ('bad thing happened', 'code is broken')
this can be done easily with this subclass
class ExceptionTemplate(Exception):
def __call__(self, *args):
return self.__class__(*(self.args + args))
# ...
class NastyError(ExceptionTemplate): pass
and if you don't like that default tuple-like representation, just add __str__
method to the ExceptionTemplate
class, like:
# ...
def __str__(self):
return ': '.join(self.args)
and you'll have
>>> raise nastyerr('code is broken')
NastyError: bad thing happened: code is broken
Upvotes: 61