You can define your own custom error handler by overriding the onError() function of your AnyLogic experiment.
The handler will be invoked every time an exception is thrown (in any action of transitions, events, dynamic events, etc.), the model execution is stopped and the engine is switched to the ERROR state.
To define the custom error handler
Add Function on the diagram of the experiment.
Note that you define function for an experiment (e.g. Simulation), not in Main or other agent type.
- Name the function onError.
- In the Arguments section of the function's properties, add the parameter of Type java.lang.Throwable. The Name of the argument can be arbitrary, say, err.
In the Function body, define the error handler code (using Java). For example, we want to output the error information in AnyLogic Console and then automatically shut down the model. To do this, write there:
traceln("An error occurred: " + err); close();
In the Advanced properties section change the type of Access to public.
How can we improve this article?