0

I'm trying to run this:

try:
    number = int(number)
except ValueError:
    raise InvalidValueError("%s is not a valid base10 number." % number)

So, when I set number = '51651a' I'm getting this:

Traceback (most recent call last):
  File "test.py", line 16, in decbin
    number = int(number)
ValueError: invalid literal for int() with base 10: '51651a'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "test.py", line 51, in <module>
    print(decbin('51651a', True))
  File "test.py", line 18, in decbin
    raise InvalidValueError("%s is not a valid base10 number." % number)
__main__.InvalidValueError: 51651a is not a valid base10 number.

My question is that is there any way I don't see the line saying, "During handling of the above exception, another exception occurred:" and all that's above it.

0

1 Answer 1

2

What you're looking for is disabling Exception Chaining with from None 1.

Change your raise statement to

raise InvalidValueError("%s is not a valid base10 number." % number) from None

and only your custom exception will be raised, without reference to the ValueError exception originally caught.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.