Catch and recover exception : Catch Exceptions « Language Basics « Python






Catch and recover exception

Catch and recover exception
Matrix = {}

Matrix[(2,3,4)] = 88

Matrix[(7,8,9)] = 99

X = 2; Y = 3; Z = 4              # ; separates statements

print Matrix[(X,Y,Z)]

print Matrix

try:
     print Matrix[(2,3,6)]       # try to index
except KeyError:                # catch and recover
     print 0



           
       








Related examples in the same category

1.Catch several exceptionsCatch several exceptions
2.Catch exception throwed inside a functionCatch exception throwed inside a function