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:

  if user_input is not in accept_values: Increase DataError (' bad user_input: {0} '. Format (user_input))   

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 ValueError ?

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:

  1. 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 KeyError .

  2. Are you sub-classing the right thing? In your example, anyone holding the ValueError will be caught DataError . Depending on your use case, there may be times where this is not suitable.

  3. 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 any or [] ) which would be more appropriate?

Comments

Popular posts from this blog

ios - Adding an SKSpriteNode to SKScene from a child SKSpriteNode -

Matlab transpose a table vector -

c# - Textbox not clickable but editable -