What is IFERROR?
IFERROR is a Microsoft Excel function that traps and handles errors. Given a formula, IFERROR will output another value in its place if the value of the formula is an error. Otherwise, it will output the value of the formula.
In Python, IFERROR is performed through exception handling.
Looking at the signature of the IFERROR function will shine more light on what IFERROR is doing:
IFERROR(value, value_if_error)
Both value and value_if_error are required arguments for the function IFERROR. If the value argument results in an error, then the value_if_error will be output.
What is considered an error for IFERROR?
Anything in the following list is considered an error for IFERROR:
- #N/A
- #VALUE!
- #REF!
- #DIV/0!
- #NUM!
- #NAME?
- #NULL!
Example of IFERROR
=IFERROR(55/0,5)
In this example, I am using 55/0 as the value argument and 5 as the value_if_error argument. Because 55/0 will result in #DIV/0!, the cell outputs the value 5.
IFERROR in Python
The Python language does not have the same function, IFERROR, to trap and handle errors from functions. What it does have is exception handling. Exception handling is a construct in many programming languages that allow developers to wrap a block of code in a try statement.
To use exception handling in Python, you wrap your code in a try/except block. The program will execute through the code in the try section. If any code causes an exception, the except block of code will then execute next.
Here is an example of the try/except in action:
try:
x = int("Not a number")
except ValueError:
print("The string of text was not a number")
You can see that the above example tries to convert the text Not a number into a valid integer. This call throws an exception, and the print statement in the except block is called.
try:
print(int(9))
except ValueError:
print("The string of text was not a number")
In the following example, I am trying to convert the number 9 to an integer, and then I want to print that integer. As this is a valid code and no exceptions are caused, the number 9 is printed to the console.
Exception terminology
When dealing with exceptions, you will hear developers use the terms throw an exception or an exception was thrown. This means that somewhere in the code, the program ran into a problem that isn’t handled, and the program has thrown an error, and it is up to the developer to catch these errors.
This concept of thrown is more evident in other programming languages where the construct of dealing with exceptions isn’t try/except but try/catch.
Custom exceptions
Now you know how to catch an exception, the next component to exceptions is being able to throw exceptions yourself. In Python, this is known as raising exceptions. Raising exceptions allows developers to force an exception to occur.
For example, I can update our previous example, and inside the try block, I can raise an exception of type ValueError.
try:
print(int(9))
raise ValueError()
except ValueError:
print("The string of text was not a number")
This example will print the value 9, as per the last example, and then it will raise the exception of type ValueError. Because the exception was raised in the try block and out except block has the exception type ValueError listed, the code will also print the exception message. Here is the output from my terminal running the above code:
Exception classes
While the previous example worked with the ValueError exception, there are other built in exceptions that developers can use as well. Each of these exceptions is an instance of the base class exception BaseException.
As a developer, you can also write your exception classes inherited from BaseException. This is especially useful when designing a more extensive system or some API.
A custom exception allows the developer to give users of their classes insight into why errors are caused when using their code. On top of just inheriting from BaseException, developers can inherit from any of the exceptions that come with Python.
The exceptions that come with Python are known as Built-in Exceptions. Inheriting these exceptions can help save time if the custom exception you’re building is similar to one of the built-in exceptions.
Printing the message from an exception
When an exception is raised from a third-party library, it is helpful to display the contents of the message written with the exception. Updating our previous example, we can add a string into the exception when raising it, and we can then access this string in the except block to log or print to the screen.
try:
print(int(9))
raise ValueError("Exception message")
except ValueError as e:
print(str(e))
This example raises the ValueError exception again, but this time, we are putting a message in the exception.
The except block is different as well. We capture the exception object (e) and then print the message’s contents with str(e).
Here is the output from running the above example:
Conclusion
This article was just a brief look at how you would perform IFERROR in Python. It is worth noting that many programming languages will have the concept of exception handling in some format or another. Exceptions are meant to be an exception to the logic of your code.
A canonical example of exception handling is when dealing with a file system, you don’t have access to a file for some reason outside your code (someone has the file open, a user deleted it, or another program moved the file).
These events might be out of the scope of a program that just needed to read the file’s contents and are, therefore, an exception to your program.