Error Handling Overview

In computer programs, some kind of errors are expected. We can simply read those errors and fix the problem. But some errors are bound to happen, usually related to user inputs or mismatch of specific conditions of the program.

Since, we know them already and knows that it would occur in some point in time during the execution, we can write codes that would respond to those errors.

These responses are error handling codes.

Python programming also supports error handling mechanism using certain keywords. These are

  1. try
  2. except
  3. else
  4. finally

Let us try to understand each one of them here.

try block

The try block contain the actual program code which gets executed in a sequential manner until an error occurs.

except block

The except block will catch the error that happened in the try block

Else block

If the try block is successful, then the program executes an else block if exists.

Finally

A piece of code that runs no matter what.

Skip to content