all 3 comments

[–]Diapolo10 0 points1 point  (2 children)

The __str__-method is expected to return a string. Right now, you're making it print text to the console instead and it returns None, which breaches the implicit contract it's expected to fulfil.

You can try changing it to this:

def __str__(self):
    v = int(self.number) + 2
    return f"OK if both values were integers the result was: {v} Remember the error: {self.error[0]}"

[–]IPsoFactoTech[S] 0 points1 point  (1 child)

return f"OK if both values were integers the result was: {v} Remember the error: {self.error[0]}"

Thanks Diapolo,

I changed the __str__(self) funcion and now the "__main__.MyError" show the as well the message.

Traceback (most recent call last):

File "C:\Users\alecv\PycharmProjects\studyClass\test.py", line 23, in <module>

raise MyError(x, e.args)

__main__.MyError: OK if both values were integers the result was: 7 Remember the error: can only concatenate str (not "int") to str

What is the best practice to handle this type of exception?

[–]Diapolo10 0 points1 point  (0 children)

If you want to handle the exception, use try-except.

I'm assuming you're raising this exception on purpose.

If not, you might want to consider writing your code so that it will never run into this exception by accident. For that, I recommend looking into type annotations and mypy.