Try or Except Structure in Python

CODE:

astr= 'Hello bob'
try:
    istr= int(astr)
except:
    istr = -1
print('First', istr)

bstr='123'
try:
    jstr= int(bstr)
except:
    jstr = -1
print('Second', jstr)

OUTPUT:

First -1
Second 123

Comments