Multiple exception handlers under one try

Multiple exception handlers under one try

Lesson Details:
June 29, 2020

I: Introduction

Introduction of python programming language

Python is an interpreted, interactive, object-oriented programming language that incorporates modules, exceptions, dynamic typing, very high level dynamic data types, and classes. Python includes tools for integration with other languages, including C, C++, Java, .Net, .DLLs, Tcl/Tk, Visual Basic, and Perl.

Python supports multiple programming paradigms including object-oriented, imperative and functional programming or procedural styles. It features a fully dynamic type system and automatic memory management and has a large and comprehensive standard library.

Python was created by Guido van Rossum in the late eighties. Most Python programmers report satisfaction with the language. However, many programmers also complain about its slow performance, especially when compared to compiled languages like C++ or Java.

II: Body

Multiple exception handlers under one try

Now we will discuss about exception handling in python. There are two types of exception handler in python. One is try - except statement. The second is raise statement. But there is also another type of exception handler in python. This is called Context Manager. It belongs to the third type of exception handler in python.

Try - Except Statement

The basic syntax of try - except statement is given below:

try: (do something) except (an exception): (do something) else: (do something)

Raising the exception:

If you want to raise exception then use raise statement as given below:

raise NameOfException()


#!/usr/bin/python3 # example of exception handling with try and except try: x=0; y=0; z=0; if (x==y): print(“x is equal to y”); else: print(“x is not equal to y”); if (z==0): print(“z is equal to 0”); else: print(“z is not equal to 0”); except NameOfException: print(“an exception has occurred”); finally: print(“this will never be printed”); print("all done");

Course content