2018-08-16 00:43:35 -04:00
|
|
|
# test try handling within a viper function
|
|
|
|
|
2023-02-01 19:51:48 -05:00
|
|
|
|
2018-08-16 00:43:35 -04:00
|
|
|
# basic try-finally
|
|
|
|
@micropython.viper
|
|
|
|
def f():
|
|
|
|
try:
|
|
|
|
fail
|
|
|
|
finally:
|
|
|
|
print("finally")
|
2020-03-22 22:26:08 -04:00
|
|
|
|
|
|
|
|
2018-08-16 00:43:35 -04:00
|
|
|
try:
|
|
|
|
f()
|
|
|
|
except NameError:
|
|
|
|
print("NameError")
|
|
|
|
|
2023-02-01 19:51:48 -05:00
|
|
|
|
2018-08-16 00:43:35 -04:00
|
|
|
# nested try-except with try-finally
|
|
|
|
@micropython.viper
|
|
|
|
def f():
|
|
|
|
try:
|
|
|
|
try:
|
|
|
|
fail
|
|
|
|
finally:
|
|
|
|
print("finally")
|
|
|
|
except NameError:
|
|
|
|
print("NameError")
|
2020-03-22 22:26:08 -04:00
|
|
|
|
|
|
|
|
2018-08-16 00:43:35 -04:00
|
|
|
f()
|
|
|
|
|
2023-02-01 19:51:48 -05:00
|
|
|
|
2018-08-16 00:43:35 -04:00
|
|
|
# check that locals written to in try blocks keep their values
|
|
|
|
@micropython.viper
|
|
|
|
def f():
|
|
|
|
a = 100
|
|
|
|
try:
|
|
|
|
print(a)
|
|
|
|
a = 200
|
|
|
|
fail
|
|
|
|
except NameError:
|
|
|
|
print(a)
|
|
|
|
a = 300
|
|
|
|
print(a)
|
|
|
|
|
2020-03-22 22:26:08 -04:00
|
|
|
|
|
|
|
f()
|