Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it OK to raise a built-in exception, but with a different message, in Python?

Tags:

Is it OK to raise a built-in exception with a custom text? or to raise a built-in warning also with custom text?

The documentation reads:

exception ValueError: Raised when a built-in operation or function receives an argument (…)

Is it implied that only built-in operations should raise a ValueError exception?

In practice, I understand that it is safe to create an exception class that inherits from ValueError or Exception. But is it OK not to do that, and directly raise a ValueError("custom text")?

Since ValueError is built-in, raising a ValueError (with a custom text) allows users to quickly see what kind of problem is involved, compared to a custom exception type (something like "ValueErrorSpecificModule", which is not standard).

like image 816
Eric O Lebigot Avatar asked Feb 01 '10 22:02

Eric O Lebigot


People also ask

What are the problems if the exception is raised?

When an exception is raised, no further statements in the current block of code are executed. Unless the exception is handled (described below), the interpreter will return directly to the interactive read-eval-print loop, or terminate entirely if Python was started with a file argument.

When should you raise an exception Python?

Use raise when you know you want a specific behavior, such as: raise TypeError("Wanted strawberry, got grape.") Raising an exception terminates the flow of your program, allowing the exception to bubble up the call stack.

Does raising an exception stop the program Python?

except block is completed and the program will proceed. However, if an exception is raised in the try clause, Python will stop executing any more code in that clause, and pass the exception to the except clause to see if this particular error is handled there.

How do you raise an exception in a message in Python?

As a Python developer you can choose to throw an exception if a condition occurs. To throw (or raise) an exception, use the raise keyword.


1 Answers

There's nothing operationally wrong with doing something like:

raise ValueError("invalid input encoding") 

In fact, I do that quite often when I'm writing the first pass of some code. The main problem with doing it that way is that clients of your code have a hard time being precise in their exception handling; in order to catch that specific exception, they would have to do string matching on the exception object they caught, which is obviously fragile and tedious. Thus, it would be better to introduce a ValueError subclass of your own; this could still be caught as ValueError, but also as the more specific exception class.

A general rule of thumb is that whenever you have code like:

raise ValueError('some problem: %s' % value) 

You should probably replace it with something like:

class SomeProblem(ValueError):     """     Raised to signal a problem with the specified value.     """ # ... raise SomeProblem(value) 

You might say that the exception type specifies what went wrong, whereas the message / attributes specify how it went wrong.

like image 90
mithrandi Avatar answered Oct 07 '22 16:10

mithrandi