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?