summaryrefslogtreecommitdiffstatshomepage
path: root/tests/basics/try_else_finally.py
diff options
context:
space:
mode:
authorDamien George <damien.p.george@gmail.com>2019-02-21 16:22:41 +1100
committerDamien George <damien.p.george@gmail.com>2019-02-21 16:22:41 +1100
commitbe41d6d6f95a7dc5874e2c6e13443bcc39d5dea8 (patch)
tree59606013e4f12598522192517c7b94ce4900a6d7 /tests/basics/try_else_finally.py
parentc72391c4ce7b07e03ac8969815b5e5c8ea626dda (diff)
downloadmicropython-be41d6d6f95a7dc5874e2c6e13443bcc39d5dea8.tar.gz
micropython-be41d6d6f95a7dc5874e2c6e13443bcc39d5dea8.zip
tests/basics: Add tests for try-except-else and try-except-else-finally.
Diffstat (limited to 'tests/basics/try_else_finally.py')
-rw-r--r--tests/basics/try_else_finally.py94
1 files changed, 94 insertions, 0 deletions
diff --git a/tests/basics/try_else_finally.py b/tests/basics/try_else_finally.py
new file mode 100644
index 0000000000..87d98bbdaf
--- /dev/null
+++ b/tests/basics/try_else_finally.py
@@ -0,0 +1,94 @@
+# test try-else-finally statement
+
+# base case
+try:
+ print(1)
+except:
+ print(2)
+else:
+ print(3)
+finally:
+ print(4)
+
+# basic case that should skip else
+try:
+ print(1)
+ raise Exception
+except:
+ print(2)
+else:
+ print(3)
+finally:
+ print(4)
+
+# uncaught exception should skip else
+try:
+ try:
+ print(1)
+ raise ValueError
+ except TypeError:
+ print(2)
+ else:
+ print(3)
+ finally:
+ print(4)
+except:
+ print('caught')
+
+# nested within outer try
+try:
+ print(1)
+ try:
+ print(2)
+ raise Exception
+ except:
+ print(3)
+ else:
+ print(4)
+ finally:
+ print(5)
+except:
+ print(6)
+else:
+ print(7)
+finally:
+ print(8)
+
+# nested within outer except, one else should be skipped
+try:
+ print(1)
+ raise Exception
+except:
+ print(2)
+ try:
+ print(3)
+ except:
+ print(4)
+ else:
+ print(5)
+ finally:
+ print(6)
+else:
+ print(7)
+finally:
+ print(8)
+
+# nested within outer except, both else should be skipped
+try:
+ print(1)
+ raise Exception
+except:
+ print(2)
+ try:
+ print(3)
+ raise Exception
+ except:
+ print(4)
+ else:
+ print(5)
+ finally:
+ print(6)
+else:
+ print(7)
+finally:
+ print(8)