# ExceptionalNumbers.py Copyright (c) Kari Laitinen # http://www.naturalprogramming.com # 2006-05-20 File created. # 2022-12-18 Converted to Python 3. # This program demonstrates how exception classes can be # defined, how exceptions can be raised ('thrown'), and how # exceptions can be caught. # Keyword pass can be used to indicate that a class definition # is 'empty', i.e., that the class is like its superclass. class NumberTooSmallException ( Exception ) : pass class NumberTooLargeException ( Exception ) : pass def get_number_from_keyboard() : number_from_keyboard = int( input() ) if number_from_keyboard <= 99 : raise NumberTooSmallException() elif number_from_keyboard >= 999 : raise NumberTooLargeException() return number_from_keyboard def get_number() : number_from_above_function = 1234 try : number_from_above_function = get_number_from_keyboard() except NumberTooSmallException : print( "\n NumberTooSmallException caught.", end="" ) return number_from_above_function # The main program begins. print( "\n Please, type in a number: ", end="" ) try : number_read_via_several_functions = get_number() print( "\n The number from keyboard is : %d" % \ number_read_via_several_functions ) except NumberTooLargeException : print( "\n NumberTooLargeException caught.", end="" ) except Exception as caught_exception : print( "\n Some Exception was caught. Some info:" ) print( " %s" % caught_exception )