hey = "bob"
try:
    print('hello')
    istr = int(hey)
    print('there')
except:
    hey= -1

print('done', hey)

$ C:/python/Python311/python.exe c:/Users/LENOVO/OneDrive/Desktop/python/hello.py hello done -1

hey = "bob"
try:
    print('hello,')
    istr = str(hey)
    print('there!')
except:
    hey = -1   
print('This is', hey.upper(), ';)')

hello, there! This is BOB ;)


a = 6
b = 2
try:
    print('resource open')
    print(a/b)
    print('resource closed')
except Exception:
    print('Inputted num is wrong')

prints with no error,

resource open

3.0

resource closed

a = 5
b = 0
try:
    print('resource open')
    print(a/b)
    print('resource closed')
except Exception:
    print('Inputted num is wrong')

as there is an error, we cannot divide a number by 0.

It prints the other statement (except).

resource open

Inputted num is wrong

a = 5
b = 0
try:
    print('resource open')
    print(a/b)
    print('resource second time open')
except Exception as a:
    print('Inputted num is wrong.', a)
    print('please try again.') 
    print('resource closed')

In this we can use exception as ‘a’.. a variable and it own says what is the error,