Python: Is There An Iserror Function?
Solution 1:
No. Python uses exceptions to handle errors, and these are caught using try...catch
blocks.
In other words, what is your definition of an error? False
? Null
? ""
?
Edit:
To answer your question in the comment, there is no well-defined way of comparing, say, "1.1"
and "1.2"
. This is intentional, because there is no obvious ordering on strings. Now in fact, Python does support comparing strings, but it uses lexicographic (alphabetical) ordering, which isn't what you want. This is because these strings are semanticallyfloat
s -- they represent numbers. Now the real problem is that they shouldn't have been strings in the first place (because they're numbers!), but sometimes you can't fix that. So instead, turn them into numbers:
float(1.1) > float(1.2)
Solution 2:
In python you do error checking with exceptions:
try:
some_function()
except Exception:
print"error"
You need to define some_function() to raise Exception
if an error happened.
Solution 3:
No you do try..except ErrorType as e: and then if you enter the exception handler, you know that the type you chose error happened. Built-in Exceptions
To compare two strings containing both numbers in same amount of decimals you can do:
print ("%20s" % first) >= ("%20s" % second)
More general way is to do normal comparison but for equality use absolute value of difference comparison.
numbers= ("1.2","1.3")
a,b= (float(num) for num in numbers)
print("Bigger or equal"if a>b else"Smaller")
Solution 4:
There are times you want to ask whether an object is an Exception (for example, when you get the result of a task back from a celery type process). You can do that using:
isinstance(x, Exception)
Post a Comment for "Python: Is There An Iserror Function?"