Disable exception chaining in python 3
up vote
10
down vote
favorite
There is a new feature that was introduced in python3 - exception chaining. For some reasons I need to disable it for certain exceptions in my code.
Here is sample code:
try:
print(10/0)
except ZeroDivisionError as e:
sys.exc_info()
raise AssertionError(str(e))
what I see:
Traceback (most recent call last):
File "draft.py", line 19, in main
print(10/0)
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "draft.py", line 26, in <module>
main()
File "draft.py", line 22, in main
raise AssertionError(str(e))
AssertionError: division by zero
what I want to see:
Traceback (most recent call last):
File "draft.py", line 26, in <module>
main()
File "draft.py", line 22, in main
raise AssertionError(str(e))
AssertionError: division by zero
I tried to use sys.exc_clear()
, but this mehtod is removed from python 3 too.
I can use workaround that works
exc = None
try:
print(10/0)
except ZeroDivisionError as e:
exc = e
if exc:
raise AssertionError(str(exc))
but I believe that there is better solution.
python python-3.x exception-handling
add a comment |
up vote
10
down vote
favorite
There is a new feature that was introduced in python3 - exception chaining. For some reasons I need to disable it for certain exceptions in my code.
Here is sample code:
try:
print(10/0)
except ZeroDivisionError as e:
sys.exc_info()
raise AssertionError(str(e))
what I see:
Traceback (most recent call last):
File "draft.py", line 19, in main
print(10/0)
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "draft.py", line 26, in <module>
main()
File "draft.py", line 22, in main
raise AssertionError(str(e))
AssertionError: division by zero
what I want to see:
Traceback (most recent call last):
File "draft.py", line 26, in <module>
main()
File "draft.py", line 22, in main
raise AssertionError(str(e))
AssertionError: division by zero
I tried to use sys.exc_clear()
, but this mehtod is removed from python 3 too.
I can use workaround that works
exc = None
try:
print(10/0)
except ZeroDivisionError as e:
exc = e
if exc:
raise AssertionError(str(exc))
but I believe that there is better solution.
python python-3.x exception-handling
add a comment |
up vote
10
down vote
favorite
up vote
10
down vote
favorite
There is a new feature that was introduced in python3 - exception chaining. For some reasons I need to disable it for certain exceptions in my code.
Here is sample code:
try:
print(10/0)
except ZeroDivisionError as e:
sys.exc_info()
raise AssertionError(str(e))
what I see:
Traceback (most recent call last):
File "draft.py", line 19, in main
print(10/0)
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "draft.py", line 26, in <module>
main()
File "draft.py", line 22, in main
raise AssertionError(str(e))
AssertionError: division by zero
what I want to see:
Traceback (most recent call last):
File "draft.py", line 26, in <module>
main()
File "draft.py", line 22, in main
raise AssertionError(str(e))
AssertionError: division by zero
I tried to use sys.exc_clear()
, but this mehtod is removed from python 3 too.
I can use workaround that works
exc = None
try:
print(10/0)
except ZeroDivisionError as e:
exc = e
if exc:
raise AssertionError(str(exc))
but I believe that there is better solution.
python python-3.x exception-handling
There is a new feature that was introduced in python3 - exception chaining. For some reasons I need to disable it for certain exceptions in my code.
Here is sample code:
try:
print(10/0)
except ZeroDivisionError as e:
sys.exc_info()
raise AssertionError(str(e))
what I see:
Traceback (most recent call last):
File "draft.py", line 19, in main
print(10/0)
ZeroDivisionError: division by zero
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "draft.py", line 26, in <module>
main()
File "draft.py", line 22, in main
raise AssertionError(str(e))
AssertionError: division by zero
what I want to see:
Traceback (most recent call last):
File "draft.py", line 26, in <module>
main()
File "draft.py", line 22, in main
raise AssertionError(str(e))
AssertionError: division by zero
I tried to use sys.exc_clear()
, but this mehtod is removed from python 3 too.
I can use workaround that works
exc = None
try:
print(10/0)
except ZeroDivisionError as e:
exc = e
if exc:
raise AssertionError(str(exc))
but I believe that there is better solution.
python python-3.x exception-handling
python python-3.x exception-handling
edited Nov 21 '15 at 0:06
Trey Hunner
6,24333153
6,24333153
asked Nov 19 '15 at 16:53
Rugnar
6741716
6741716
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
up vote
17
down vote
accepted
Simple Answer
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from None
However, you probably actually want:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from e
Explanation
__cause__
Implicit exception chaining happens through __context__
when there isn't an explicit cause exception set.
Explicit exception chaining works through __cause__
so if you set __cause__
to the exception itself, it should stop the chaining. If __cause__
is set, Python will suppress the implicit message.
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
exc.__cause__ = exc
raise exc
raise from
We can use "raise from" to do the same thing:
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
raise exc from exc
None __cause__
Setting __cause__
to None
actually does the same thing:
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
exc.__cause__ = None
raise exc
raise from None
So that brings us to the most elegant way to do this which is to raise from None
:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from None
But I would argue that you usually want to explicitly raise your exception from the cause exception so the traceback is preserved:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from e
This will give us a slightly different message that states that the first exception was the direct cause of the second:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
AssertionError: division by zero
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
17
down vote
accepted
Simple Answer
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from None
However, you probably actually want:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from e
Explanation
__cause__
Implicit exception chaining happens through __context__
when there isn't an explicit cause exception set.
Explicit exception chaining works through __cause__
so if you set __cause__
to the exception itself, it should stop the chaining. If __cause__
is set, Python will suppress the implicit message.
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
exc.__cause__ = exc
raise exc
raise from
We can use "raise from" to do the same thing:
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
raise exc from exc
None __cause__
Setting __cause__
to None
actually does the same thing:
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
exc.__cause__ = None
raise exc
raise from None
So that brings us to the most elegant way to do this which is to raise from None
:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from None
But I would argue that you usually want to explicitly raise your exception from the cause exception so the traceback is preserved:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from e
This will give us a slightly different message that states that the first exception was the direct cause of the second:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
AssertionError: division by zero
add a comment |
up vote
17
down vote
accepted
Simple Answer
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from None
However, you probably actually want:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from e
Explanation
__cause__
Implicit exception chaining happens through __context__
when there isn't an explicit cause exception set.
Explicit exception chaining works through __cause__
so if you set __cause__
to the exception itself, it should stop the chaining. If __cause__
is set, Python will suppress the implicit message.
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
exc.__cause__ = exc
raise exc
raise from
We can use "raise from" to do the same thing:
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
raise exc from exc
None __cause__
Setting __cause__
to None
actually does the same thing:
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
exc.__cause__ = None
raise exc
raise from None
So that brings us to the most elegant way to do this which is to raise from None
:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from None
But I would argue that you usually want to explicitly raise your exception from the cause exception so the traceback is preserved:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from e
This will give us a slightly different message that states that the first exception was the direct cause of the second:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
AssertionError: division by zero
add a comment |
up vote
17
down vote
accepted
up vote
17
down vote
accepted
Simple Answer
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from None
However, you probably actually want:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from e
Explanation
__cause__
Implicit exception chaining happens through __context__
when there isn't an explicit cause exception set.
Explicit exception chaining works through __cause__
so if you set __cause__
to the exception itself, it should stop the chaining. If __cause__
is set, Python will suppress the implicit message.
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
exc.__cause__ = exc
raise exc
raise from
We can use "raise from" to do the same thing:
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
raise exc from exc
None __cause__
Setting __cause__
to None
actually does the same thing:
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
exc.__cause__ = None
raise exc
raise from None
So that brings us to the most elegant way to do this which is to raise from None
:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from None
But I would argue that you usually want to explicitly raise your exception from the cause exception so the traceback is preserved:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from e
This will give us a slightly different message that states that the first exception was the direct cause of the second:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
AssertionError: division by zero
Simple Answer
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from None
However, you probably actually want:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from e
Explanation
__cause__
Implicit exception chaining happens through __context__
when there isn't an explicit cause exception set.
Explicit exception chaining works through __cause__
so if you set __cause__
to the exception itself, it should stop the chaining. If __cause__
is set, Python will suppress the implicit message.
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
exc.__cause__ = exc
raise exc
raise from
We can use "raise from" to do the same thing:
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
raise exc from exc
None __cause__
Setting __cause__
to None
actually does the same thing:
try:
print(10/0)
except ZeroDivisionError as e:
exc = AssertionError(str(e))
exc.__cause__ = None
raise exc
raise from None
So that brings us to the most elegant way to do this which is to raise from None
:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from None
But I would argue that you usually want to explicitly raise your exception from the cause exception so the traceback is preserved:
try:
print(10/0)
except ZeroDivisionError as e:
raise AssertionError(str(e)) from e
This will give us a slightly different message that states that the first exception was the direct cause of the second:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
ZeroDivisionError: division by zero
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 4, in <module>
AssertionError: division by zero
answered Nov 20 '15 at 9:00
Trey Hunner
6,24333153
6,24333153
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f33809864%2fdisable-exception-chaining-in-python-3%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown