I wanted to know how to hide errors in python: Let's say I made a calculator and then typed:
if number1 != int:
print("NaN")
But it will print this message and give out the error which is built into python which is: Traceback (most recent call last):
but how do I hide this Traceback error and only show the error message which is "NaN"
Thank you for the answer.
Even though you are talking about try...except
, the following statement makes no sense.
You telling python to compare 1 to data type int
if number1 != int: print("NaN")
If you want to check a particular data type, use isinstance(<variable>,<data type>)
if isinstance(number1,int): print("NaN")
You can use try...except
method to catch various errors:
try:
number1=int(input("Enter a number: "))
...
except ValueError:
print("NaN")
Note that this will catch only ValueError
. If you want to catch all errors, use except Exception
or except: