diff --git a/docs/exceptions_handling.md b/docs/exceptions_handling.md index a6a61bf..5f4242e 100644 --- a/docs/exceptions_handling.md +++ b/docs/exceptions_handling.md @@ -106,6 +106,22 @@ This way, in turn, can also be shortened, as it is overly popular: The trick, though, works with strings only, and always provide `Exception` instances, which is good for debugging but most often not enough. +## finally block + +If `finally` block present, it will be executed after body (until first exception) +and catch block, if any will match. finally statement is executed even if the +exception will be thrown and not caught locally. It does not alter try/catch block result: + + try { + } + finally { + println("called finally") + } + >>> called finally + >>> void + +- and yes, there could be try-finally block, no catching, but perform some guaranteed cleanup. + # Custom error classes _this functionality is not yet released_ diff --git a/library/build.gradle.kts b/library/build.gradle.kts index 38f2ce3..97df154 100644 --- a/library/build.gradle.kts +++ b/library/build.gradle.kts @@ -5,7 +5,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl import org.jetbrains.kotlin.gradle.dsl.JvmTarget group = "net.sergeych" -version = "0.5.1-SNAPSHOT" +version = "0.5.2-SNAPSHOT" buildscript { repositories { diff --git a/library/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt b/library/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt index 87d8920..b46757e 100644 --- a/library/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt +++ b/library/src/commonMain/kotlin/net/sergeych/lyng/Compiler.kt @@ -790,8 +790,6 @@ class Compiler( t = cc.next() } } - if( catches.isEmpty() ) - throw ScriptError(cc.currentPos(), "try block must have at least one catch clause") val finallyClause = if( t.value == "finally" ) { parseBlock(cc) } else { @@ -799,6 +797,9 @@ class Compiler( null } + if( catches.isEmpty() && finallyClause == null ) + throw ScriptError(cc.currentPos(), "try block must have either catch or finally clause or both") + return statement { var result: Obj = ObjVoid try {