Python Forum
Changing a traceback message without a 2nd raise
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Changing a traceback message without a 2nd raise
#1
1/0
Error:
Traceback (most recent call last): File "C:/Documents/err.py", line 9, in <module> 1/0 ZeroDivisionError: division by zero
I want to change exactly one thing about this stack trace, that message that follows the colon.
I do not want to raise a 2nd Error because I need to preserve the ability to click back through in the shell.

I've tried capturing it:
try:
    1/0
except ZeroDivisionError:
    raise ZeroDivisionError("Don't do that")
But this makes the traceback too complex and the last click back is to the raise, not the actual error.
Error:
Traceback (most recent call last): File "C:/Documents/err.py", line 10, in <module> 1/0 ZeroDivisionError: division by zero During handling of the above exception, another exception occurred: Traceback (most recent call last): File "C:/Documents/err.py", line 12, in <module> raise ZeroDivisionError("Don't do that") ZeroDivisionError: Don't do that
I imagine the answer is in the traceback module but I cannot find it.

What I want is:
Error:
Traceback (most recent call last): File "C:/Documents/err.py", line 9, in <module> 1/0 ZeroDivisionError: Don't do that
Reply
#2
Try
try:
    1/0
except ZeroDivisionError as err:
    err.args = ("Don't do that",)
    raise
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  raise exception within generator bermudj 3 2,897 Jun-06-2020, 11:56 AM
Last Post: buran
  can i raise an exception in a try clause? Skaperen 14 5,600 Dec-19-2019, 12:29 AM
Last Post: Skaperen
  raise exception instead of return None Skaperen 4 4,048 Jul-19-2018, 01:27 AM
Last Post: ichabod801
  "Raise SMTPException("SMTP AUTH extension not supported by server.") NajeebUllah 3 7,919 Mar-16-2018, 09:45 PM
Last Post: nilamo

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020