0

Ref: Earlier post at Don't show Python raise-line in the exception stack that does not adequately answer this question.

In Python 3.7.3 on the Raspberry Pi, (latest available for this platform) raising an exception causes two things to happen: 1. The actual line of code that raises the exception is listed in the traceback. 2. Next, the actual exception and text that I want to raise is printed.

IMHO this is both messy and confusing to a potential user of my code. Ideally what should happen is that the traceback should identify the exception, and, (if possible) what line of code caused the exception to occur - like the built-in exception handler does. This is simpler, cleaner, and much easier to understand

    if num_die > 5:
        raise ValueError('num_die should not exceed 5. The value of num_die was: {}'.format(num_die))
    elif num_throws > 20:
        raise ValueError('num_throws should not exceed 20. The value of num_die was: {}'.format(num_throws))
    elif num_sides == 4 or num_sides == 6 or num_sides == 8 or num_sides == 12 or num_sides == 20:
        return int(num_die), int(num_sides), throws
    else:
        raise ValueError('A fair die can only have 4, 6, 8, 12, or 20 sides. The number of sides you requested was: {}'.format(num_sides))

causes the following to occur when run:

Traceback (most recent call last):
  File "./Function_Test.py", line 18, in <module>
    i = (throw_dice(num_throws=25, num_sides=6))
  File "./Function_Test.py", line 11, in throw_dice
    raise ValueError('num_throws should not exceed 20. The value of num_throws was: {}'.format(num_throws))
ValueError: num_throws should not exceed 20. The value of num_throws was: 25
pi@Pi-4Gig:~/Python $ 

Ideally, the traceback should not include both the line of code that raises the exception. Viz.:

    raise ValueError('num_throws should not exceed 20. The value of num_throws was: {}'.format(num_throws))

along with the actual exception that I want to be thrown. Viz.:

ValueError: num_throws should not exceed 20. The value of num_throws was: 25

(could not use second code block here, sorry)

Is this possible without hacking system characteristics somewhere?

Jim JR Harris
  • 413
  • 1
  • 6
  • 15
  • 1
    Don't mess with this. It's not worth the confusion you'll introduce. You may consider this behavior confusing, but it'll be more confusing for people to have to read weird, customized tracebacks. – user2357112 Oct 18 '19 at 22:24
  • 1
    if you're dead set on this you can do, `import sys; sys.tracebacklimit = 0` and then the traceback won't come up, but I echo @user2357112 's sentiment that this is not a good idea – gold_cy Oct 18 '19 at 22:26
  • Ok, I get it - messing with sleeping dragons is a non-optimal choice. – Jim JR Harris Oct 19 '19 at 23:07
  • The real question here becomes "why is this still such a prevalent problem?" It's ugly as heck, makes the program developer look like an incompetent fool to his user-base, and can confuse the user tremendously. I would have raised this as a "severity-1 - priority-1" bug a long time ago. – Jim JR Harris Oct 19 '19 at 23:16
  • P.S. How do I mark a reply as "the answer" to my question? I agree with both comments and would like to mark the first one as "the answer". – Jim JR Harris Oct 19 '19 at 23:21

0 Answers0