programming inbbm101/fall18/files/... · 2018-12-14 · programming in hacettepe university...

17
Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Upload: others

Post on 12-Jul-2020

0 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Programming in

Hacettepe University

Computer Engineering Department

BBM103 Introduction to Programming Lab 1

Week 10

Fall 2018

Page 2: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Exceptions

Page 3: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Built-in ExceptionsThe simplest way to handle exceptions is with a "try-except" block:

Output: divide by zero

Example 1:

Page 4: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Example 2: except ValueError:

Page 5: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Example 3: except ZeroDivisionError:

Page 6: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Example 4: except (ValueError, ZeroDivisionError)

Page 7: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Example 5: try... except... as...

Page 8: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Example 6: try... except... else...

Page 9: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Example 7: try... except... finally...

Page 10: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Some Examples using Exceptionsexcept IOError:

print('An error occured trying to read the file.')

except ValueError: print('Non-numeric data found in the file.')

except ImportError: print ("NO module found«)

except EOFError: print('Why did you do an EOF on me?')

except KeyboardInterrupt:print('You cancelled the operation.')

except: print('An error occured.')

Page 11: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

raiseExample 8:

Page 12: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Example 9:

Page 13: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Example 10: User-Defined exceptions

This example continues in the next slide

Page 14: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Example 10 continued

Page 15: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Assert

Output:

Example 11:

assert <some_test>, <message>

Page 16: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Example 12:

Output:

Page 17: Programming inbbm101/fall18/files/... · 2018-12-14 · Programming in Hacettepe University Computer Engineering Department BBM103 Introduction to Programming Lab 1 Week 10 Fall 2018

Example 13:

Output: