Downsides of subclassing specific exception in Python? -
I have a situation where a user can provide poor data to get data data that does not exist . At present, when these problems occur then error types are raised or not clear
In my API, I intend to make an error to capture in the UI (or I want to give it to the UI Framework Users will be able to run it by reporting directly, which I have tested and works): DataError (ValueError): Increase this error with the message of 'wrong', General ValueErrors Or other exceptions ValueError subclass '' '/ code> and usage: I have no opinion about my approach, and I need an unnecessary code. I know about fluctuation, because I can catch this specific exception without hiding others. But I can not think of any downsides. What happens to this as opposing to just raising my error message with Write a custom exception is almost always the right thing to do, with the exception that the sub-classes There are some minor potential downsides to: Is your error ok for an error in the standard library? For example, executing an object that looks like a word, you just want to throw Are you sub-classing the right thing? In your example, anyone holding the Do sub-clasings provide you additional functionality? This is a class, it can be of all kinds. If you sub-class exception, you should confirm that all its methods are understood for your error status. In other circumstances, the exception can not be perfect at all. Is there a 'no value' result (such as
if user_input is not in accept_values: Increase DataError (' bad user_input: {0} '. Format (user_input))
ValueError ?
KeyError .
ValueError will be caught
DataError . Depending on your use case, there may be times where this is not suitable.
any or
[] ) which would be more appropriate?
Comments
Post a Comment