Java exception handling

In order to catch Java™ exceptions within programs, use a TRY/CATCH block.

When a Java exception occurs, the runtime system sets the STATUS variable to the error code -8306.

The Java exception details (i.e. the name of the exception) can be found with the ERR_GET(STATUS) built-in function.
Important: To get the Java exception type with ERR_GET(), do not execute other instructions before querying for the error message, otherwise the STATUS variable might be reset to zero and the Java exception details would be lost.

To easily identify the type of the Java exceptions in your code, consider writing a library function based on ERR_GET(), that recognizes most common Java exceptions, and converts them to integer codes:

IMPORT JAVA java.lang.StringBuffer
MAIN
  DEFINE sb java.lang.StringBuffer
  LET sb = StringBuffer.create("abcdef")
  TRY
    CALL sb.deleteCharAt(50) -- out of bounds!
  CATCH
    DISPLAY err_get(STATUS)
    EXIT PROGRAM 1
  END TRY
END MAIN
Note: As a general pattern, do not use TRY/CATCH or WHENEVER ERROR CONTINUE exception handlers if no exception is supposed to occur. By default the program will then stop and display the Java exception details.