A try statement embeds a set of statements and makes it possible for your code to handle an exception that occurs when those statements run.
In most cases, the first block that matches the exception is the block that runs. However, AnyException is the most general kind of exception, and any block reserved for AnyException is the last to be considered.
If an onException block runs, the exception is handled, and processing continues after the try block.
If a statement causes an exception and is not embedded in a try statement, the exception propagates to the invoking function and potentially to yet higher-level functions, as suggested earlier.
If none of the statements in a try statement cause an exception or invoke a function from which an exception is propagated, none of the onException blocks in the try block run.
Use of a try block slows processing, so you might include only statements that are more likely than others to cause an exception. Examples include database-access statements and service invocations.
You can code a throw statement to explicitly cause an exception, which is then processed as any other exception is processed. For details, see “throw statement.”
The following try statement embeds a statement that accesses a database, but could access several statements:
try add myPayment to ds; onException(ex SQLException) SysLib.writeStdOut(ex.message); onException(ex AnyException) SysLib.writeStdOut(ex.message); end
The code responds to the exception by writing a message that is provided in the exception record. Other tasks might include rolling back a logical unit of work.
Target | Issue |
---|---|
Java | No issues |
JavaScript | No issues. |