What is Error Handling and Why is it Essential to Programs?

by Carson
error handling

Sometimes, the best way to deal with an error is to work around it and move on. This is used in error handling, which is an essential mechanism in many programs, and is what we will discuss in this article.

The Purpose of Error Handling

If a program encounters an error, it usually stops functioning and quits. All unsaved work will be lost. That is because the program will have no way to proceed since it fails to determine the value of a particular variable or cannot parse the instruction. However, if the program encounters an issue at runtime, the program may be able to deal with it.

This is where error handling comes in. In some cases, you might think, “Isn’t prevention the best cure?” This is the case for many types of errors, but you cannot prevent them from being produced in some cases. For instance, if an app fails to connect to the network, it cannot determine the value of a specific variable. Without error handling, the program crashes. You don’t want an app to crash multiple times due to unstable network connections, right? As a result, the program must detect this error and display a message that tells the user to reconnect instead.

Moreover, these statements can also help programmers debug. For instance, it can be used to send debugging reports before the program quits, enabling developers to debug their code and release a better version of their software.

How Can Error Handling Be Implemented

Error handling is implemented using try-catch blocks. A program tries to do something, and catches an exception if it is produced. After catching the exception, it does something in the catch box.

Some programming languages, like C++ and JavaScript, simply use the “try” and “catch” keywords. However, there is no keyword known as “catch” in Python. Another keyword, which is “except”, is used instead.

    print(2 / 0) #Throws ZeroDivisionError
except ZeroDivisionError:
    print("Division by zero is not allowed!")

The Limitations of Error Handling

However, error handling has its limitations, too. For instance, it cannot handle code that contains syntax errors. It does not even know what instructions to create, so the program fails at the compilation stage. Therefore, error handling can only catch errors at runtime. Shown below is a snippet of C++ code that produces a syntax error upon compilation due to a missing semicolon. This error might seem insignificant, but it means that the compiler, which relies on specific templates, cannot parse the program.

#include <iostream>
int main(){
    int var = 1 //missing semicolon

Even though Python is an interpreted language, it cannot handle syntax errors as well. Running the code below demonstrates this. Instead of printing the statement in the catch block, it simply throws a syntax error and stops its execution.

    $print(2) #Throws SyntaxError
except SyntaxError:
    print("A '$' is not allowed in a Python statement!")

Moreover, in some cases it’s simply not useful to use error handling. It should only be used if the error is intermittent, and it’s better to fix the errors altogether if it appears every time you run the program.


In this article, we’ve briefly discussed what error handling is, how should it be used, and how it can sometimes fail. For more information, please visit the webpages in the references below.

References and Credits

  1. (n.d.). Error Handling. Retrieved April 9, 2022, from https://www.techopedia.com/definition/16626/error-handling
  2. (n.d.). Error Handling. Retrieved April 9, 2022, from https://www.techtarget.com/searchsoftwarequality/definition/error-handling

Related Posts

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.