Error Handling Overview

Advertisements
Advertisements

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.

Advertisements

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.


Bestseller

Python Programming: Using Problem Solving Approach

Python Programming is designed as a textbook to fulfil the requirements of the first-level course in Python programming. It is suited for undergraduate degree students of computer science engineering, information technology as well as computer applications. This book will enable students to apply the Python programming concepts in solving real-world problems. The book begins with an introduction to computers, problem solving approaches, programming languages, object oriented programming and Python programming. Separate chapters dealing with the important constructs of Python language such as control statements, functions, strings, files, data structures, classes and objects, inheritance, operator overloading and exceptions are provided in the book. Each chapter ends with objective-type questions, review questions, programming and debugging exercises to facilitate revision and practice of concepts learnt.


Advertisements
Advertisements
Advertisements