#!/usr/bin/python

from types import *

class ErreurNegIndice( Exception ):

    def __init__( self, idx ):
        self.str = "Indice negatif: " + str( idx )

#    def __init__( self ):
#        self.str = "Indice negatif"

    def __str__( self ):
        return self.str


class ErreurIndiceTropGrand( Exception ):
    str1 = "Indice "
    str2 = "trop grand, depasse du tableau"

    def __init__( self, idx ):
        self.str1 = self.str1 + str( idx ) + " "

    def __str__( self ):
        return self.str1 + self.str2



def lireValeurIndice( tab, idx ):
    if type( idx ) != IntType:
        raise TypeError( idx )
    elif idx > len( tab) - 1:
        raise ErreurIndiceTropGrand( idx )
    elif idx < 0:
        raise ErreurNegIndice( idx )
    else:
        return tab[idx]

def main():
    tableau = [ 4, 6, 2 ]
    try:
        lireValeurIndice( tableau, 5 )
    except Exception, e:
        print e

    try:
        lireValeurIndice( tableau, -2 )
    except ErreurNegIndice, e:
        print e
    except ErreurIndiceTropGrand, e:
        print e
    else:
        print "Autre erreur - erreur de type inconnu"

    try:
        lireValeurIndice( tableau, "a" )
    except Exception, e:
        print type( e )
        print e
        print "erreur"


if __name__ == "__main__":
    main()
