I am quite a newbie understanding of how to catch exceptions in python. I have a question regarding those two types of ways of catching exceptions. I only found useful information about ValidationError regarding here
But I did not quite understand if it can be used besides django or what are the error messages that I can expect about it. I saw this code sample regarding the validation of types.
except (TypeError, ValueError) as error:
LOGGER.error('Error e.g.', exc_info=True)
except ValidationError:
LOGGER.error('Error e.g', exc_info=True)
So for TypeError and ValueError for me, it is clear:
exception ValueError
Raised when an operation or function receives an argument that has the right type but an inappropriate value, and the situation is not described by a more precise exception such as IndexError.
exception TypeError
Raised when an operation or function is applied to an object of inappropriate type. The associated value is a string giving details about the type mismatch.
In conclusion,
I am trying to understanding what would be the advantage of the second code with ValidationError, but it could be tricky as I did not find good documentation about. If someone could share knowledge about ValidationError, I would highly appreciate,
I am raising this question because I am going to use related library and I have not seen the exceptions being treated like this.
https://pypi.org/project/related/
Thank you community!
TypeErrorandValueErrorare built into Python. But a library can and often will define its own exceptions.ValidationErrorcomes from a library you are using.