Home > OS >  ValueError exception doesn't do anything
ValueError exception doesn't do anything

Time:10-13

I have made a script that includes an exception for ValueError, however when the exception does not run when there is a ValueError

Value Error Exception

What happens when I run it

CodePudding user response:

The condition that raises the "ValueError" exception should be inside the try except block. In this case the function that raise the exception is int() because you try to convert a letter 'e' to an integer.

So you should do something like this:


try:
 ans= int(input("Answer:")) # <--- this raises the exception
 do_something()
except ValueError:
 print("This is raised if the argument of int() function is a string")

CodePudding user response:

As shown in this MDN Doc you should use the exceprionVar parameter into the catch block and then check if (exceptionVar instanceof ValueError)

  • Related