We are developing the next version of CrazyEngineers. If you wish to receive latest updates and early access, click the link below.
How to use custom parameters in custom exceptions in Python?
I am trying to implement and understand custom exceptions in python. I create a base class like below:
class SDKException(Exception): """Generic exception."""
def __init__(self, *args, **kwargs): """SDKException object creation."""
# what if custom attributes here in this __init__? super().__init__(self, *args, **kwargs)
And then I have this inheriting exception class:
class RequestError(SDKException): """Request error occured."""
def __init__(self, *args, **kwargs): """Initialize RequestError with additional parameters."""
# maybe custom attributes here # self.default_message = ...
super().__init__(*args, **kwargs) # maybe custom attributes here # self.timestamp = ...
I am struggling with understanding of customization of such exceptions. Like, for example, everything could be caught by just Exception (which is bad), then we have ValueError and other sub exceptions. So how would such sub exceptions understand it is the time for them to be thrown and how would they use additional information (also considering the inheritance RequestError -> SDKException -> Exception).
I am struggling with understanding of such inheritance, additional parameters, args, kwargs, why they are needed, how they are used, how exceptions know it's their time to fire.Posted in: #Python