In [20]:
class cello :
pass
strings = ('"hello"',
'hello',
'"hello" + 2',
'"hello"[5]',
'{"hello": 0}["hi"]',
'cello.pitch',
'open("hello.txt")')
from random import choice
try:
string = choice(strings)
print(string)
exec(string)
finally:
print('Output in any case')
In [23]:
class cello :
pass; exec(cello.pitch)
In [ ]:
strings = ('"hello"',
'hello',
'"hello" + 2',
'"hello"[5]',
'{"hello": 0}["hi"]',
'class cello : {} ; cello.pitch',
'open("hello.txt")')
from random import choice
try:
string = choice(strings)
print(string)
exec(string)
except NameError:
print
print('NameError!')
except TypeError as e:
print('TypeError!')
print(e)
except (IndexError, KeyError):
print('IndexError or KeyError!')
except:
print('Any other error...')
else:
print('All good, no error.')
finally:
print('Output in any case')
In [ ]:
try:
hello
except NameError:
try:
'hello'[5]
except IndexError:
try:
{"hello": 0}["hi"]
except KeyError as e:
raise ZeroDivisionError from e
In [ ]:
try:
1 / 0
except ZeroDivisionError:
raise ZeroDivisionError('I shall not divide by 0!') from None