Error Handling

In kaoscript, the error handling is very Java-like with some go-iness.

try..catch

syntax

try {
...statements
}
[on classname1 [catch varname]] {
...statements
}]
[on classname2 [catch varname]] {
...statements
}]
...
[on classnameN [catch varname]] {
...statements
}]
[catch [varname] {
...statements
}]
[finally {
...statements
}]

examples

try {
console.log('foobar')
}
on RangeError catch error {
console.log('RangeError', error)
}
catch error {
console.log('Error', error)
}
finally {
console.log('finally')
}
try {
console.log('foobar')
}
on RangeError {
console.log('RangeError')
}
catch {
console.log('Error')
}

disabling

koascript allows to disable the disable the default error handling.

global

#![error(off)]
func foobar() ~ Error {
throw new Error(`Not Implemented`)
}
func disabled() {
foobar()
}

local

func foobar() ~ Error {
throw new Error(`Not Implemented`)
}
#[error(off)]
func qux() {
foobar()
}

The error handling is only disabled for the function qux.

filtering

You can also exclude error type that need to be handled.

#[error(ignore(SyntaxError))]
#[error(ignore(SyntaxError, UnexpectedError))]