Hi all,
If an exception is raised in a script (using try and catch), how to ensure that the change made as a result of the script up till the line of the error is rolled back?
Thanks,
Maisy
Just throw your own exception or a string like:
try { ...} catch (e) { ... throw "This script failed fro some reason. No changes were applied";}
Happy Scrpting
Jan
Hi All a small update .
I have been also using approach to return Status object (used bind() to map the class into the script) but since Since 2025 R1 there is easier way how to create Status object.
As described in her ... Is it possible to add to the "Problems" view from a script? an new classes are added into the script context Issue and Issues
here the focus is on the Issue class and its STATIC methods that return Status
IStatus Issue.info(String message)IStatus Issue.warning(String message)IStatus Issue.error(String message)
So If script will return error status ( Status with severity 4) the effect is the same as in case of using throw =. script operation will be rolled back
//pre 2025R1try {...} catch (e){ Status(4,"Error from script","Script operation rolled back because of problem",java.lang.Throwable(e));}//since 2025R1try { ...} catch (e) {//access the Status class, create new one with plugin, message, and throwable Issue.error("").class.(4,"Error from script","Script operation rolled back because of problem",java.lang.Throwable(e))}//simpler method no plugin/Throwable ... just the message try { ...} catch (e) { Issue.error("Error from script, Script operation rolled back because of problem\n"+e);}
Happy rolling-back,
Marian
Thanks marian, great comment. One small addition. There is also this one:
Issue.error(String message, Throwable exception)Issue.warning(String message, Throwable exception)
which makes the second option easier to achieve.
Yes, happy rolling