refs #22 try without catch but finally

This commit is contained in:
Sergey Chernov 2025-06-13 01:44:13 +04:00
parent 0981d8370e
commit 88974e0f2d
3 changed files with 20 additions and 3 deletions

View File

@ -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 The trick, though, works with strings only, and always provide `Exception` instances, which is good for debugging but
most often not enough. 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 # Custom error classes
_this functionality is not yet released_ _this functionality is not yet released_

View File

@ -5,7 +5,7 @@ import org.jetbrains.kotlin.gradle.ExperimentalWasmDsl
import org.jetbrains.kotlin.gradle.dsl.JvmTarget import org.jetbrains.kotlin.gradle.dsl.JvmTarget
group = "net.sergeych" group = "net.sergeych"
version = "0.5.1-SNAPSHOT" version = "0.5.2-SNAPSHOT"
buildscript { buildscript {
repositories { repositories {

View File

@ -790,8 +790,6 @@ class Compiler(
t = cc.next() 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" ) { val finallyClause = if( t.value == "finally" ) {
parseBlock(cc) parseBlock(cc)
} else { } else {
@ -799,6 +797,9 @@ class Compiler(
null null
} }
if( catches.isEmpty() && finallyClause == null )
throw ScriptError(cc.currentPos(), "try block must have either catch or finally clause or both")
return statement { return statement {
var result: Obj = ObjVoid var result: Obj = ObjVoid
try { try {