Overview

Introduction

Welcome to the Handling Exceptions Practice Lab. In this module, you will be provided with the instructions and devices needed to develop your hands-on skills.

Learning Outcomes

In this module, you will complete the following exercise:

  • Exercise 1 - Working with Exception Handling

After completing this lab, you will be able to:

  • Handle exceptions in Python programs

Exam Objectives

The following exam objectives are covered in this lab:

  • Analyze, detect, and fix code segments that have errors
  • Analyze and construct code segments that handle exceptions

Lab Duration

It will take approximately 20 minutes to complete this lab.

Exercise 1 - Working with Exception Handling

Exception handling involves adding provisions to handle exceptional/abnormal conditions in a program. Such conditions, if not handled, forces the program to crash. You need to include exception-handling elements in the program to change the normal flow of program execution. A robust program will take care of all possible exceptions.

In Python, you can handle exceptions using the try…except block. The try…except block works as follows:

  • First, the try clause or the code between try and except clause is executed.
  • If there is no exception, then only try clause runs.
  • If any exception occurs, try clause is skipped, and except clause is run.
  • If any exception occurs, but the except clause within the code doesn’t handle it, it is passed on to the outer try statements. If the exception is left unhandled, then the execution stops.
  • A try statement can have more than one except clause.

In this exercise, you will perform the following tasks:

  • Task 1 - Download Calculator.py program file from the Intranet and open it in the IDLE environment.
  • Task 2 - Add exception-handling code in Calculator.py program.
  • Task 3 - Download WordCount.py program file from the Intranet and add exception-handling code in it.

Learning Outcomes

After completing this exercise, you will be able to:

  • Handle exceptions in Python programs

Comprehensive Learning

See the full benefits of our immersive learning experience with interactive courses and guided career paths.