65
PYTHON PROGRAMMING X. EXCEPTION HANDLING Engr. Ranel O. Padon

Python Programming - X. Exception Handling and Assertions

Embed Size (px)

Citation preview

Page 1: Python Programming - X. Exception Handling and Assertions

PYTHON PROGRAMMING

X. EXCEPTION HANDLING

Engr. Ranel O. Padon

Page 2: Python Programming - X. Exception Handling and Assertions

PYTHON PROGRAMMING TOPICS

I• Introduction to Python Programming

II• Python Basics

III• Controlling the Program Flow

IV• Program Components: Functions, Classes, Packages, and Modules

V• Sequences (List and Tuples), and Dictionaries

VI• Object-Based Programming: Classes and Objects

VII• Customizing Classes and Operator Overloading

VIII• Object-Oriented Programming: Inheritance and Polymorphism

IX• Randomization Algorithms

X• Exception Handling and Assertions

XI• String Manipulation and Regular Expressions

XII• File Handling and Processing

XIII• GUI Programming Using Tkinter

Page 3: Python Programming - X. Exception Handling and Assertions
Page 4: Python Programming - X. Exception Handling and Assertions
Page 5: Python Programming - X. Exception Handling and Assertions

DefensiveProgramming

try

except

else

finally

raise

assert

class Exception

Page 6: Python Programming - X. Exception Handling and Assertions
Page 7: Python Programming - X. Exception Handling and Assertions

http://www.newser.com/story/20282/china-atm-glitch-nets-man-life.html

Page 8: Python Programming - X. Exception Handling and Assertions

http://www.newser.com/story/148427/atm-error-lets-man-withdraw-15m-gamble-it-away.html

Page 9: Python Programming - X. Exception Handling and Assertions

INTRODUCTION

Exception is an indication of a special event that

occurs during a program’s execution.

Exception indicates that, although the event can occur,

the event occurs infrequently.

Page 10: Python Programming - X. Exception Handling and Assertions

INTRODUCTION

Page 11: Python Programming - X. Exception Handling and Assertions

INTRODUCTION

When a Python script encounters a situation that

it can't cope with, it raises an exception.

An exception is a Python object that represents an error.

Page 12: Python Programming - X. Exception Handling and Assertions

INTRODUCTION

Page 13: Python Programming - X. Exception Handling and Assertions

INTRODUCTION

Page 14: Python Programming - X. Exception Handling and Assertions

INTRODUCTION | Exceptions

1. Division by Zero

2. Addition of two incompatible types.

3. Accessing a file that is nonexistent.

4. Accessing a nonexistent index of a sequence.

5. Deleting a table in a disconnected database server.

6. Withdrawing money greater than the available amount.

7. Invalid input bearing angle.

8. Negative perimeter distance.

9. …

Page 15: Python Programming - X. Exception Handling and Assertions

INTRODUCTION | Exceptions

Resource Leak

Aborting a program component could leave a file or a network

connection in a state in which other programs

are not able to acquire the resource.

Page 16: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING

Page 17: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING

Advantages

1. handle the error

- catching the exception

- resolving the exception

2. continue processing as if no error had occured

3. programs are more clear, robust and more fault-tolerant

Page 18: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING

When no exceptions occur, exception-handling code

incurs little or no performance penalties.

Programs that implement exception handling operate more

efficiently than programs that perform error handling throughout

the program logic.

Page 19: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING

Brute-Force Error Handling

Page 20: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING

Brute-Force Error Handling

it may work, but intermixing the logic of the program

with the error-handling logic can make the program difficult

to read, modify, maintain and debug—especially in large

applications.

Page 21: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING

Exception handling enables the programmer to

remove error-handling code from the “main line”

of the program’s execution.

This improves program clarity and enhances modifiability.

Page 22: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | Python

Mechanism is similar with that used in Modula-3, C# and Java.

Not all programming languages support exception handling.

Page 23: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING

Page 24: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | try-except

Page 25: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | try-except

Python uses try statements to enable exception handling.

The try statement encloses statements

that potentially cause exceptions.

A try statement consists of keyword try, followed by a colon

(:), followed by a suite of code in which exceptions may occur,

followed by one or more clauses.

Page 26: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | try-except

Immediately following the try suite may be

one or more except clauses

(also called except handlers).

Each except clause specifies zero or more

exception names that represent the type(s)

of exceptions the except clause can handle.

Page 27: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | try-except

If an exception occurs in a try suite,

the try suite expires and program control

transfers to the first matching except handler

(if there is one) following the try suite.

Page 28: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | try-except

an except handler always should specify the name

of the exception to catch.

an empty except handler should be used only for a

default catch-all case.

Page 29: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | try-except

If no exceptions occur in a try suite,

the interpreter ignores the exception handlers

for that try statement.

Page 30: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | try-except

Page 31: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | else

After the last except clause, an optional else clause

contains code that executes if the code in

the try suite raised no exceptions.

Page 32: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | Summary

Page 33: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | Summary

Page 34: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | Summary

Page 35: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | Summary

Page 36: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | finally

A try suite can be followed by zero except clauses;

in that case, it must be followed by a finally clause.

The code in the finally suite always executes,

regardless of whether an exception occurs.

Page 37: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | finally

Programs frequently request and release resources dynamically.

Programs that obtain certain types of resources (such as files)

sometimes must return those resources explicitly to the system

to avoid resource leaks.

Page 38: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | finally

The finally clause is an ideal location to place

resource deallocation code for resources acquired.

Page 39: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | finally

Page 40: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | finally

Page 41: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | finally

Page 42: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | finally

Page 43: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | Exception

Exceptions are objects of classes that inherit

from class Exception.

Programmer-defined exception classes should

derive directly or indirectly from class Exception.

Page 44: Python Programming - X. Exception Handling and Assertions

class Exception

Page 45: Python Programming - X. Exception Handling and Assertions

class Exception

(1/2)

Page 46: Python Programming - X. Exception Handling and Assertions

class Exception

(2/2)

Page 47: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | raise

Raising/Throwing An Exception

raise keyword

Catching and handling exceptions enables a

program to know when an error has occurred, then to take

actions to minimize the consequences of that error.

Page 48: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | raise

The raise statement may specify an argument or

arguments that initialize the exception object.

In this case, a comma follows the exception name, and the

argument or a tuple of arguments follows the comma.

Page 49: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | raise Args

Page 50: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | raise Args

Page 51: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | Custom

Page 52: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | Custom

Page 53: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | Custom

Page 54: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | assert

An assertion is a sanity-check that you can turn on/off

when you are done with your testing of the program.

Assertion is like a raise-if statement

(or to be more accurate, a raise-if-not statement).

An expression is tested, and if the result comes up false,

an exception is raised.

Page 55: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | assert

Programmers often place assertions

at the start of a function to check for valid input,

and after a function call to check for valid output.

Page 56: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | assert

Page 57: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | assert

Page 58: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | assert

Page 59: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | assert

Page 60: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | assert

Page 61: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING | assert

Page 62: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING

Page 63: Python Programming - X. Exception Handling and Assertions

EXCEPTION HANDLING

Page 64: Python Programming - X. Exception Handling and Assertions

REFERENCES

Deitel, Deitel, Liperi, and Wiedermann - Python: How to Program (2001).

Disclaimer: Most of the images/information used here have no proper source

citation, and I do not claim ownership of these either. I don’t want to reinvent the

wheel, and I just want to reuse and reintegrate materials that I think are useful or

cool, then present them in another light, form, or perspective. Moreover, the

images/information here are mainly used for illustration/educational purposes only,

in the spirit of openness of data, spreading light, and empowering people with

knowledge.

Page 65: Python Programming - X. Exception Handling and Assertions

Thank You!