Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer...

14
Exceptions CMSC 201

Transcript of Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer...

Page 1: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Exceptions

CMSC 201

Page 2: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Overview

Exceptions are run-time errors, especially ones

that the programmer cannot predict.

example 1: division by zero

example 2: user enters "garbage" data

example 3: disk full

Page 3: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Vocabulary

When some piece of code causes a run-time error,

we say that the code throws an exception or that it

raises an exception.

The part of a program that deals with the run-time

error catches the exception or handles the

exception.

Page 4: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Divide by Zero

totalBill = 67

n = int(input("Number of people? "))

share = totalBill / n

print("Share of the bill is ", share)

If user enters 0, Python complains and terminates:

Traceback (most recent call last):

File "divide_by_zero.py", line 3, in <module>

share = totalBill / n

ZeroDivisionError: division by zero

Page 5: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Exception Handling

try:

totalBill = 67

n = int(input("Number of people? "))

share = totalBill / n

except ZeroDivisionError :

print("Customers ran away")

else:

print("Share of the bill is ", share)

Page 6: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Syntax for Exceptions

try:

block of code that might causeone or more types of exceptions

except ExceptionType1 :block of code to handle ExceptionType1

except ExceptionType2 :block of code to handle ExceptionType2

...else:

block of code to execute when no exceptions found

Page 7: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Exception Types

How to find exception types?

1. Read the friendly manual (RTFM)

2. Google (really, same as RTFM)

3. Make Python tell you:

Traceback (most recent call last):

File "divide_by_zero.py", line 3, in <module>

share = totalBill / n

ZeroDivisionError: division by zero

Page 8: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Exception Types

>>> userInput = input("Enter a number: ")

Enter a number: abc

>>> n = int(userInput)

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

ValueError: invalid literal for int() with base 10: 'abc'

>>>

Page 9: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Exception Types

>>> userInput = input("Enter a number: ")

Enter a number: ^D

Traceback (most recent call last):

File "<stdin>", line 1, in <module>

EOFError

Page 10: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Badgering the user for input

done = False

while not done: try: userInput = input("Enter a number: ") n = int(userInput) except ValueError: print("That's not an integer! Try again.") else: print("Thank you!") done = True

print("n is ", n)

Page 11: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Badgering the user for input

done = False

while not done: try: userInput = input("Enter a number: ") n = int(userInput) except ValueError: print("That's not an integer! Try again.") else: print("Thank you!") done = True

print("n is ", n)

Page 12: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Badgering the user for input

done = False

while not done: try: userInput = input("Enter a number: ") n = int(userInput) except ValueError: print("That's not an integer! Try again.") except EOFError: print("Please type something! Try again.") else: print("Thank you!") done = True

print("n is ", n)

Page 13: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

Raising an ExceptionYou can write code that raises exceptions:

try: raise ZeroDivisionError

except ZeroDivisionError: print("Did someone divide by zero?")

else: print("Everything is hunky-dory")

More useful later when we look at functions

Page 14: Exceptions CMSC 201. Overview Exceptions are run-time errors, especially ones that the programmer cannot predict.  example 1: division by zero  example.

BaseException• The BaseException type matches all

exceptions, even ones you don't know about.

• Use this very carefully! Might not be a good

idea.

• What can you do if you catch a BaseException?

o exit the program slightly more gracefully.

o return to home state (if this is possible).

o re-throw the exception (requires more syntax

and not clear what is accomplished).