Home
Categories
Dictionary
Download
Project Details
Changes Log
FAQ
License

Exceptions handling



By default exceptions encountered in the script will be rethrown as ScriptException. These exceptions may be encountered in several cases: It is possible to log exceptions rather than throw them to the caller by:
      wrapper.logExceptions(true);
By default exception messages will be send to the err stream, but it is possible to control how the exceptions are logged by implementing the ScriptLogger interface and setting the logger to the wrapper by:
      wrapper.setScriptLogger(<the logger>);

Exception listener

One or several ScriptExceptionListeners can be added to the wrapper with:
      wrapper.addExceptionListener(<the listener>);
The exception listener has three methods:
  • The notified(String message, Throwable th) method notifies the listener for any exception encountered during the execution of the script
  • The notifiedFirst(ScriptRuntimeException exception, String extension) method notifies the listener for the first exception encountered during the execution of the script. The provided notifiedFirst(ScriptRuntimeException allows to get the StackTrace with optionnaly only the script stack elements
  • The aborted(String message) method notifies the listener for an abort of the script
Additionnaly, the getScriptState() method return the state of the listener: Note that by default this method will always return STATE_OK. It is the responsability of the listener to implement correctly the notified, notifiedFirst, aborted, and resetState methods to be sure that the listener return its state correctly.

Showing exceptions in a code editor

Main Article: Swing utility classes

It is possible to show a Swing component which will show the exceptions in a code Editor[1]
You will need to have the scriptHelperSwing.jar file in your Classpath
. To show the exceptions in a Swing component, just use the following:
      ScriptWrapper<Script> wrapper = new GroovyScriptWrapper() {
      }
      ...
      SwingExceptionListener listener = new SwingExceptionListener();
      wrapper.addExceptionListener(listener);
Then the first exception encountered during the compilation or execution of the script will popup the following window:
swingError
Note that the SwingExceptionListener handles correctly the getScriptState() method.

Notes

  1. ^ You will need to have the scriptHelperSwing.jar file in your Classpath

Categories: api

Copyright 2019-2020 Herve Girod. All Rights Reserved. Documentation and source under the BSD licence