Questions tagged [raise]
237 questions
590
votes
15 answers
Best practice for using assert?
Is there a performance or code maintenance issue with using assert as part of the standard code instead of using it just for debugging purposes?
Is
assert x >= 0, 'x is less than zero'
better or worse than
if x < 0:
raise Exception('x is less…

meade
- 22,875
- 12
- 32
- 36
330
votes
6 answers
How to use "raise" keyword in Python
I have read the official definition of "raise", but I still don't quite understand what it does.
In simplest terms, what is "raise"?
Example usage would help.

Capurnicus
- 8,171
- 5
- 18
- 11
180
votes
4 answers
How to use pytest to check that Error is NOT raised
Let's assume we have smth like that :
import py, pytest
ERROR1 = ' --- Error : value < 5! ---'
ERROR2 = ' --- Error : value > 10! ---'
class MyError(Exception):
def __init__(self, m):
self.m = m
def __str__(self):
return…

paraklet
- 1,887
- 2
- 13
- 6
177
votes
4 answers
How to re-raise an exception in nested try/except blocks?
I know that if I want to re-raise an exception, I simple use raise without arguments in the respective except block. But given a nested expression like
try:
something()
except SomeError as e:
try:
plan_B()
except AlsoFailsError:
…

Tobias Kienzler
- 25,759
- 22
- 127
- 221
92
votes
9 answers
What's the difference between raise, try, and assert?
I have been learning Python for a while and the raise function and assert are (what I realised is that both of them crash the app, unlike try - except) really similar and I can't see a situation where you would use raise or assert over try.
So, what…

Defneit
- 1,041
- 1
- 8
- 6
58
votes
2 answers
Rails ActiveSupport: How to assert that an error is raised?
I am wanting to test a function on one of my models that throws specific errors. The function looks something like this:
def merge(release_to_delete)
raise "Can't merge a release with itself!" if( self.id == release_to_delete.id )
raise "Can…

spilliton
- 3,811
- 5
- 35
- 35
53
votes
3 answers
TypeError:exceptions must be old-style classes or derived from BaseException, not str
Following is my code:
test = 'abc'
if True:
raise test + 'def'
And when i run this, it gives me the TypeError
TypeError: exceptions must be old-style classes or derived from BaseException, not str
So what kind of type should the test be?

2342G456DI8
- 1,819
- 3
- 16
- 29
35
votes
5 answers
raise statement on a conditional expression
How do I elegantly implement the "Samurai principle" (return victorious, or not at all) on my functions?
return if else raise

F3RD3F
- 2,309
- 3
- 22
- 26
30
votes
2 answers
How to imitate Python 3's raise ... from in Python 2?
Python 3 has the neat
try:
raise OneException('sorry')
except OneException as e:
# after a failed attempt of mitigation:
raise AnotherException('I give up') from e
syntax which allows raising a followup exception without loosing…

Tobias Kienzler
- 25,759
- 22
- 127
- 221
27
votes
3 answers
Is it OK to raise a built-in exception, but with a different message, in Python?
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…

Eric O. Lebigot
- 91,433
- 48
- 218
- 260
25
votes
3 answers
Don't show Python raise-line in the exception stack
When I raise my owns exceptions in my Python libraries, the exception stack shows the raise-line itself as the last item of the stack. This is obviously not an error, is conceptually right, but points the focus on something that is not useful for…

Htechno
- 5,901
- 4
- 27
- 37
21
votes
4 answers
Python reraise/recatch exception
I would like to know if it is possible in python to raise an exception in one except block and catch it in a later except block. I believe some other languages do this by default.
Here is what it would look like"
try:
something
except…

Aaron Robinson
- 406
- 1
- 6
- 13
21
votes
1 answer
Raise an event in C#
I came across this question in a Microsoft Practice Test and I got confused. Here is the question:
Which of the following C# code samples
is the proper way to raise an event,
assuming that the Alarm event, the
AlarmEventArgs class, and the
…

Long Ngo
- 385
- 1
- 2
- 8
17
votes
3 answers
Proper way of raising events from C++/CLI?
I was wondering what's the proper way of raising events from C++/CLI. In C# one should first make a copy of the handler, check if it's not null, and then call it. Is there a similar practice for C++/CLI?

Filip Frącz
- 5,881
- 11
- 45
- 67
14
votes
5 answers
C#: Do you raise or throw an exception?
I know that this probably doesn't really matter, but I would like to know what is correct.
If a piece of code contains some version of throw new SomeKindOfException(). Do we say that this piece of code can potentially raise an exception? Or throw an…

Svish
- 152,914
- 173
- 462
- 620