Error handling

Try catch statement

By default, if an error was received at the time of the script execution, the script immediately finishes its work. If you want to avoid the termination of the script, you should use the try statement. If an error occurs during the execution of the code inside the try block, the control will pass to the catch block, which must be after try. After the catch keyword, it is necessary to specify the name of the variable of error type, which will contain information about the error. You can use [special functions] (https://gentee.github.io/stdlib/runtime#erriderror-err-int) to get the identifier and error text. If you do not remove the error inside catch with recover or retry, it will be passed on and the script will finish its work.

TryStmt ="try" Block CatchStmt
CatchStmt = "catch" identifier Block
run  {
   try {
      myfunc()
      error(101, "Custom error")
   }
   catch err {
      if ErrID(err) != 101:  error( 102, "Error \{ErrText(err)} has occurred in myfunc()")
   } 
}

Recover statement

The recover statement is used inside a catch block to remove the error. This command removes the error information, the script exits the current catch block and continues execution.

RecoverStmt = "recover"
run str {
   try : 10/0
   catch err :  recover
   return "ok"
} 
// ok

Retry statement

The retry statement is used inside a catch block to restart try. This command removes the error information and the script re-executes the corresponding try block.

RetryStmt = "retry"
run {
   str fname
   try {
       fname = ReadString("Specify filename: ")
       Println("Beginning of the file: ", str(ReadFile(fname, 0, 50)))
    } catch err {
       Println("ERROR #\{ErrID(err)}: \{ErrText(err)}")
       retry
    }
}

Last updated