0

How do I tell which error types (e.g. TypeError, ValueError) to use?

I know I can choose, however are some more relevant than others according to context?

Example: failing to add an edge to a graph due to node rank limit (I want it to be an error rather than just a boolean signaling the failure)

3
  • Are you looking to throw a custom error if something goes wrong? Commented Apr 29, 2020 at 12:42
  • @Treatybreaker I prefer to use the common error types (with a message) and not create custom ones, if that's what you're asking... Unless it is considered bad practice, in which case I'd love to know that. Commented Apr 29, 2020 at 12:52
  • Well unless you're getting an error that is caused by failing to add an edge, you're going to want to throw your own error which can be done through raise Exception("Your exception") See: docs.python.org/3/tutorial/errors.html which may help you out. Commented Apr 29, 2020 at 12:59

2 Answers 2

1

You can find the descriptions of all the default exceptions here: https://docs.python.org/3/library/exceptions.html

For instance,

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.

This exception may be raised by user code to indicate that an attempted operation on an object is not supported, and is not meant to be. If an object is meant to support a given operation but has not yet provided an implementation, NotImplementedError is the proper exception to raise.

Passing arguments of the wrong type (e.g. passing a list when an int is expected) should result in a TypeError, but passing arguments with the wrong value (e.g. a number outside expected boundaries) should result in a ValueError.

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.

However, sometimes, it makes sense to think about defining a custom Exception. In your case, it could be

class GraphError(Exception):
    pass

raise GraphError("Node foo - rank limit exceeded")

or

class LinkError(Exception):
    message = "Node {node} - rank limit exceeded"

    def __init__(self, node):
        super(LinkError, self).__init__(
            self.message.format(
                node=node,
            )
        )
        self.node = node

raise LinkError('foo')
Sign up to request clarification or add additional context in comments.

Comments

1

failing to add an edge to a graph due to node rank limit.

The closest buildin exception that i can think of is either IndexError or RunTimeError, you can find all buildin exceptions and more info about exceptions on this page.

You can also make your own exception type by inheriting from Exception. See this tutorial page about creating your own exception.

class RankLimitReached(Exception):
    pass

raise RankLimitReached

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.