Tuesday, 15 October 2013

Week 3 2013

In this week class, we learn about inheritance and exception. Inheritance is the ability to define a new class that is a modified version of an existing class. The new class inherits all of the methods of the existing lass and if we add the new method in child class will not affect the parent class. We also learn about exception, Whenever a runtime error occurs, it creates an exception object. The program stops running at this point and Python prints out the trace back, which ends with a message describing the exception that occurred.
import time
from week2test import Stack
def get_age():
    age = int(input("please enter your age:"))
    if age < 0:
        my_error = ValueError("{0} is not a valid age".format(age))
        raise my_error
    return age

'''
for i in range (1,101):
    print (i)
'''

def error_try():
    start = time.time()
 
    results_list = []
    errors_list = []

    for i in range(-10,10):
        try:
            val = 100/i
            print(val)
        except ZeroDivisionError as Err:
            errors_list.append('Expected Error :' + str(Err))
            print('Error on number :' + str(i))
        except Exception as Err:
            errors_list.append('Unexpected Error :' + str(Err))
            print('Error on number :' + str(i))
        else:
            results_list.append(val)

    print(results_list)
    print(errors_list)

    end = time.time()

    print (end-start)
         
#error_try()

class IntStack(Stack):
    '''
    a sub class of Stack only accept integer
    '''

    def push(self: 'IntStack', n: int) -> None:
        if isinstance(n, int):
            Stack.push(self,n)
        else:
            raise PushNonIntegerException('not a integer')

    def pop(self: 'IntStack') -> int:
        #note for the empty list
        if self.is_empty():
            raise PopEmptyStackException
        else:
            Stack.pop(self)
            #use super class method

class PushNonIntegerException(Exception):
    pass

class PopEmptyStackException(Exception):
    pass

'''
its = IntStack()

try:
 
    its.push(1)
    print(its)
    its.push(2)
    print(its)
    its.push('2')
    print(its)
    its.push('sd')
    its.pop()
    print(its)

except PushNonIntegerException as Err:
    print('Error1')
except PopEmptyStackException as Err:
    print('Error2')
except Exception as Err:
    print('Unknown Error')
'''


class SpecialException(Exception):
    pass

class ExtremeException(SpecialException):
    pass


if __name__ == '__main__':
    try:
        raise ExtremeException('i m an extremeexception')
        raise SpecialException('i m a specialexception')
        #raise Exception('I am an Exception')

    except ExtremeException as ee:
        print(ee)
        print ('caugh as extremeexception')
        raise

    except SpecialException as se:
        print (se)
        print ('caught as specialexcetion')
        raise
    except Exception as ep:
        print(ep)
        print('caugh as excetion')
        raise      

No comments:

Post a Comment