What is an Exception? Exception Handling. What is an Exception? What is an Exception? test = [1,2,3] test[3]

Similar documents
Introduction to: Computers & Programming: Exception Handling

Exception Handling. Genome 559

BBM 102 Introduction to Programming II Spring 2017

BBM 102 Introduction to Programming II Spring Exceptions

Python Essential Reference, Second Edition - Chapter 5: Control Flow Page 1 of 8

Class extension and. Exception handling. Genome 559

CIS192 Python Programming

CIS192 Python Programming

Class extension and. Exception handling. Genome 559

CSE : Python Programming. Homework 5 and Projects. Announcements. Course project: Overview. Course Project: Grading criteria

CIS192 Python Programming

COMP1730/COMP6730 Programming for Scientists. Exceptions and exception handling

Review 3. Exceptions and Try-Except Blocks

Le L c e t c ur u e e 5 To T p o i p c i s c t o o b e b e co c v o e v r e ed e Exception Handling

Exceptions CS GMU

LECTURE 4 Python Basics Part 3

CSc 120. Introduction to Computer Programming II. 07: Excep*ons. Adapted from slides by Dr. Saumya Debray

Outline. the try-except statement the try-finally statement. exceptions are classes raising exceptions defining exceptions

Exception Handling and Debugging

4.3 FURTHER PROGRAMMING

Introduction to Computer Programming for Non-Majors

Computer Science 9608 (Notes) Chapter: 4.3 Further programming

FILE HANDLING AND EXCEPTIONS

TESTING, DEBUGGING, EXCEPTIONS, ASSERTIONS

Introduction to Computer Programming for Non-Majors

Chapter 9: Dealing with Errors

CS 11 python track: lecture 2

Getting Started with Python

Python for Informatics

Errors. And How to Handle Them

Introduction to Computer Programming for Non-Majors

Exceptions & a Taste of Declarative Programming in SQL

DEBUGGING TIPS. 1 Introduction COMPUTER SCIENCE 61A

Fundamentals of Programming (Python) Getting Started with Programming

Object Oriented Programming

#11: File manipulation Reading: Chapter 7

1 Lecture 6: Conditionals and Exceptions

MEIN 50010: Python Flow Control

Introduction to python

Exceptions & error handling in Python 2 and Python 3

Introduction to Python and Programming. 1. Python is Like a Calculator. You Type Expressions. Python Computes Their Values /2 2**3 3*4+5*6

Python File Modes. Mode Description. Open a file for reading. (default)

Algorithms and Programming

Lecture #12: Quick: Exceptions and SQL

Files. Files need to be opened in Python before they can be read from or written into Files are opened in Python using the open() built-in function

COMP519 Web Programming Lecture 20: Python (Part 4) Handouts

CIS192 Python Programming

Lecture 21. Programming with Subclasses

Python Tutorial. Day 2

Control Structures 1 / 17

CS Programming Languages: Python

Computer Science 217

1 Classes. 2 Exceptions. 3 Using Other Code. 4 Problems. Sandeep Sadanandan (TU, Munich) Python For Fine Programmers May 16, / 19

Fundamentals of Object Oriented Programming

Python for Astronomers. Errors and Exceptions

Lecture 21. Programming with Subclasses

CS Lecture 26: Grab Bag. Announcements

Advanced Python. Executive Summary, Session 1

Decision structures. A more complex decision structure is an if-else-statement: if <condition>: <body1> else: <body2>

Exception Handling. Chapter 9

Python Programming: An Introduction to Computer Science

Object Oriented Programming

Class definition. F21SC Industrial Programming: Python. Post-facto setting of class attributes. Class attributes

COMP 204: Sets, Commenting & Exceptions

Chapter 5 : Informatics practices. Conditional & Looping Constructs. Class XI ( As per CBSE Board)

Try and Error. Python debugging and beautification

Exception Handling in Java. An Exception is a compile time / runtime error that breaks off the

Exceptions, Case Study-Exception handling in C++.

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

Lecture 18. Classes and Types

Interactive use. $ python. >>> print 'Hello, world!' Hello, world! >>> 3 $ Ctrl-D

AP COMPUTER SCIENCE JAVA CONCEPTS IV: RESERVED WORDS

61A Lecture 26. Monday, October 31

EXCEPTIONS, CALCULATOR 10

21. Exceptions. Advanced Concepts: // exceptions #include <iostream> using namespace std;

F21SC Industrial Programming: Python: Classes and Exceptions

CSC System Development with Java. Exception Handling. Department of Statistics and Computer Science. Budditha Hettige

Introduction to Problem Solving and Programming in Python.

Exceptions. Exceptions. Can have multiple except suites and/or one unnamed except suite

SCHEME INTERPRETER GUIDE 4

Introduction to programming using Python

Basic Concepts. Computer Science. Programming history Algorithms Pseudo code. Computer - Science Andrew Case 2

CMSC 201 Fall 2016 Lab 09 Advanced Debugging

Lecture 21. Programming with Subclasses

Question 1. tmp = Stack() # Transfer every item from stk onto tmp. while not stk.is_empty(): tmp.push(stk.pop())

file_object=open( file_name.txt, mode ) f=open( sample.txt, w ) How to create a file:

Debugging & Errors Why you were up till 2AM

What we already know. more of what we know. results, searching for "This" 6/21/2017. chapter 14

COMP 204: Sets, Commenting & Exceptions

12. Logical Maneuvers. Topics: Loop-Body Returns Exceptions Assertions Type Checking Try-Except

Hacettepe University Computer Engineering Department. Programming in. BBM103 Introduction to Programming Lab 1 Week 4. Fall 2018

Object Oriented Programming Exception Handling

Positional, keyword and default arguments

Exception Handling. Sometimes when the computer tries to execute a statement something goes wrong:

Exception Handling. Run-time Errors. Methods Failure. Sometimes when the computer tries to execute a statement something goes wrong:

Abstract Data Types. CS 234, Fall Types, Data Types Abstraction Abstract Data Types Preconditions, Postconditions ADT Examples

National University. Faculty of Computer Since and Technology Object Oriented Programming

Introduction to Python

CPSC 427: Object-Oriented Programming

ECE 364 Software Engineering Tools Lab. Lecture 8 Python: Advanced I

Transcription:

What is an Exception? Exception Handling BBM 101 - Introduction to Programming I Hacettepe University Fall 2016 Fuat Akal, Aykut Erdem, Erkut Erdem An exception is an abnormal condition (and thus rare) that arises in a code sequence at runtime. For instance: Dividing a number by zero Accessing an element that is out of bounds of an array Attempting to open a file which does not exist When an exceptional condition arises, an object representing that exception is created and thrown in the code that caused the error An exception can be caught to handle it or pass it on 1 Exceptions can be generated by the run-time system, or they can be manually generated by your code 2 test = [1,2,3] test[3] What is an Exception? IndexError: list index out of range What is an Exception? successfailureratio = numsuccesses/numfailures print('the success/failure ratio is', successfailureratio) print('now here') ZeroDivisionError: integer division or modulo by zero 3 4

What is an Exception? Handling Exceptions val = int(input('enter an integer: ')) print('the square of the number you entered is', val**2) Exception mechanism gives the programmer a chance to do something against an abnormal condition. > Enter an integer: asd Exception handling is performing an action in response to an exception. ValueError: invalid literal for int() with base 10: 'asd' This action may be: Exitingthe program Retryingthe action with or without alternativedata Displaying an error message and warning user to do something... 5 6 Handling Exceptions Handling Exceptions successfailureratio = numsuccesses/numfailures print('the success/failure ratio is', successfailureratio) Except ZeroDivisionError: print('no failures, so the success/failure is undefined.') print('now here') Upon entering the try block, the interpreter attempts to evaluate the expression numsuccesses/numfailures. If expression evaluation is successful, the assignment is done and the result is printed. If, however, a ZeroDivisionError exception is raised, the print statement in the except block is executed. while True: val = input('enter an integer: ') val = int(val) print('the square of the number you entered is', val**2) break #to exit the while loop except ValueError: print(val, 'is not an integer') Checks for whether ValueError exception is raised or not 7 8

Keywords of Exception Handling There are five keywords in Python to deal with exceptions: try, except, else, raise and finally. Creates a block to monitor if any exception occurs. except: Follows the try block and catches any exception which is thrown within it. Are There Many Exceptions in Python? Yes, some of them are Exception ArithmeticError OverflowError ZeroDivisonError EOFError NameError IOError SyntaxError 9 List of all exceptions (errors): http://docs.python.org/3/library/exceptions.html#bltin-exceptions 10 Multiple except Statements It is possible that more than one exception can be thrown in a code block. We can use multiple except clauses When an exception is thrown, each except statement is inspected in order, and the first one whose type matches that of the exception is executed. Type matching means that the exception thrown must be an object of the same class or a sub-class of the declared class in the except statement After one except statement executes, the others are bypassed. 11 Multiple except Statements You do your operations here; except Exception-1: Execute this block. except Exception-2: Execute this block. except (Exception-3[, Exception-4[,...ExceptionN]]]): If there is any exception from the given exception list, then execute this block. except (ValueError, TypeError): The except block will be entered if any of the listed exceptions is raised within the try block 12

Multiple except Statements Multiple except Statements f = open('outfile.dat', 'w') dividend = 5 divisor = 0 division = dividend / divisor f.write(str(division)) except IOError: print("i can't open the file!") except ZeroDivisionError: print("you can't divide by zero!") f = open('outfile.dat', 'w') dividend = 5 divisor = 0 division = dividend / divisor f.write(str(division)) except Exception: print("exception occured and handled!") except IOError: print("i can't open the file!") except ZeroDivisionError: print("you can't divide by zero!") You can't divide by zero! 13 Exception occured and handled! 14 Multiple except Statements except-else Statements f = open('outfile.dat', 'w') dividend = 5 divisor = 0 division = dividend / divisor f.write(str(division)) except: print("exception occured and handled!") except IOError: print("i can't open the file!") except ZeroDivisionError: print("you can't divide by zero!") SyntaxError: default 'except:' must be last You do your operations here except: Execute this block. else: If there is no exception then execute this block. f = open(arg, 'r') except IOError: print('cannot open', arg) else: print(arg, 'has', len(f.readlines()), 'lines') 15 16

finally Statement finally creates a block of code that will be executed after a try/except block has completed and before the code following the try/except block finally block is executed whether or not exception is thrown finally block is executed whether or not exception is caught finally Statement You can use it to clean up files, database connections, etc. You do your operations here except: Execute this block. finally: This block will definitely be executed. It is used to gurantee that a code block will be executed in any condition. 17 file = open('out.txt', 'w') do something finally: file.close() os.path.remove('out.txt') 18 Nested try Blocks When an exception occurs inside a try block; If the try block does not have a matching except, then the outer try statement s except clauses are inspected for a match If a matching except is found, that except blockis executed If no matching except exists, execution flow continues to find a matching except by inspectingthe outer try statements If a matching except cannot be found at all, the exception will be caught by Python s exception handler. Execution flow never returns to the linethat exception was thrown. This means, an exception is caught and except block is executed, the flow will continue with the lines followingthis except block 19 Let s clarify it on various scenarios Information: Exception1 and Exception2 are subclasses of Exception3 Question: Which statements are executed if 1- throws Exception1 2- throws Exception1 3- throws Exception3 4- throws Exception1 and throws Exception2 20

Scenario: throws Exception1 Step1: Exception is thrown Exception1 Step2: except clauses of the try block are inspected for a matching except statement. Exception3 is super class of Exception1, so it matches. Step3: is executed, exception is handled and execution flow will continue bypassing the following exceptclauses Step4: statement9 is executed 21 Scenario: throws Exception1 Step1: Exception is thrown Exception1 Step2: except clauses of the try block are inspected for a matching except statement. First clause catches the exception Step3: is executed, exception is handled Step4: execution flow will continue bypassing the following except clauses. is executed. Step5: Assuming no exception is thrown by, program continues with statement7 and statement9. 22 Scenario: throws Exception3 Step1: Exception is thrown Exception3 Step2: except clauses of the try block are inspected for a matching except statement. None of these except clauses match Exception3 Step3: except clauses of the outer try statement are inspected for a matching except. Exception3 is catched and is executed Scenario: throws Exception1 and throws Exception2 Step1: Exception is thrown Exception1 Step2: Exception is catched and is executed. Step3: throws a new exception Exception2 Step4: Except clauses of the outer try statement are inspected for a matching except. Exception2 is catched and is executed Step4: statement9 is executed 23 Step5: statement9 is executed 24

raise Statement You can raise exceptions by using the raise statement. The syntax is as follows: raise exceptionname(arguments) raise Statement def getratios(vect1, vect2): """ Assumes: vect1 and vect2 are equal length lists of numbers Returns: a list containing the meaningful values of vect1[i]/vect2[i] """ ratios = [] for index in range(len(vect1)): ratios.append(vect1[index]/vect2[index]) except ZeroDivisionError: ratios.append(float('nan')) #nan = Not a Number except: raise ValueError( getratios called with bad arguments ) return ratios 25 print(getratios([1.0, 2.0, 7.0, 6.0], [1.0,2.0,0.0,3.0])) print(getratios([], [])) print(getratios([1.0, 2.0], [3.0])) [1.0, 1.0, nan, 2.0] except ValueError as msg: [] print(msg) getratios called with bad arguments 26 raise Statement Avoid raising a generic Exception! To catch it, you'll have to catch all other more specific exceptions that subclass it.. def demo_bad_catch(): raise ValueError('a hidden bug, do not catch this') raise Exception('This is the exception you expect to handle') except Exception as error: print('caught this error: ' + repr(error)) >>> demo_bad_catch() caught this error: ValueError('a hidden bug, do not catch this',) raise Statement and more specific catches won't catch the general exception:.. def demo_no_catch(): raise Exception('general exceptions not caught by specific handling') except ValueError as e: print('we will not catch e') >>> demo_no_catch() Traceback (most recent call last): File "<stdin>", line 1, in <module> File "<stdin>", line 3, in demo_no_catch Exception: general exceptions not caught by specific handling 27 28

Custom Exceptions Users can define their own exception by creating a new class in Python. This exception class has to be derived, either directly or indirectly, from Exception class. Most of the built-in exceptions are also derived form this class. 29 Custom Exceptions class ValueTooSmallError(Exception): """Raised when the input value is too small""" pass class ValueTooLargeError(Exception): """Raised when the input value is too large""" pass number = 10 # you need to guess this number while True: i_num = int(input("enter a number: ")) if i_num < number: raise ValueTooSmallError elif i_num > number: raise ValueTooLargeError break except ValueTooSmallError: print("this value is too small, try again!") except ValueTooLargeError: print("this value is too large, try again!") print("congratulations! You guessed it correctly.") 30