#.require. jvm # mods for unreachable code and using RuntimeException rather than Exception namespace Test class Test def main is shared x as int = 0 try if true, throw RuntimeException('') x = 1 # unreachable code catch nre as NonNilCastException # Cobra RTL exception CobraCore.noOp(nre) x = 1 catch x = 2 assert x==2 try x = 0 throw RuntimeException('') catch RuntimeException # no variable x = 1 assert x==1 loc as int count as int # rethrow an exception try loc = 1 try loc = 2 throw RuntimeException('aoeu') catch loc = 3 throw catch e as RuntimeException loc = 4 assert e.message=='aoeu' assert loc==4 # test that the "success" block executes when no exception is thrown try loc = 1 count = 1 catch loc = 2 count += 1 success loc = 3 count += 1 assert loc==3 assert count==2 # test the the "catch" block does not execute when no exception is throwd try loc = 1 count = 1 catch loc = 2 count += 1 success loc = 3 count += 1 finally loc = 4 count += 1 assert loc==4 assert count==3 # test that the "success" block does not execute when an exception is throwd try loc = 1 count = 1 throw RuntimeException('') catch loc = 2 count += 1 success loc = 3 count += 1 assert loc==2 assert count==2 # just for the heck of it try pass catch pass success pass finally pass # this actually failed code gen at one point try pass success pass finally pass # test that `success` and `finally` execute even with a `return` in the try part .reset .returnInTry assert not _didCatch and _didSuccess and _didFinally # test that `finally` executes even with an exception .reset expect RuntimeException, .throwInSuccess assert not _didCatch and _didSuccess and _didFinally var _didCatch as bool is shared var _didSuccess as bool is shared var _didFinally as bool is shared def reset is shared _didCatch = _didSuccess = _didFinally = false def returnInTry is shared try return # test that a return statement here doesn't side-step `success` or `finally` catch _didCatch = true throw success _didSuccess = true finally _didFinally = true def throwInSuccess is shared try pass success _didSuccess = true throw Exception('aoeu') # test that an exception here doesn't side-step `finally` finally _didFinally = true