Sparx Systems Forum

Enterprise Architect => Automation Interface, Add-Ins and Tools => Topic started by: Guillaume on August 06, 2019, 08:14:11 pm

Title: throw / catch exceptions with JScript
Post by: Guillaume on August 06, 2019, 08:14:11 pm
Hi,
I have a JScript script that calls a number of functions e.g.
main >
     f1
     f2
           f2.1
           f2.2

When I run f2.1, I'd like to stop the script if a condition is verified. I'm trying to do that with exceptions but EA doesn't let me create an "Exception". I also tried to throw a random value or a null -> I'm getting a non catch exception error...
Is there any way to stop a script from a any function?

function f2()
{
   try
   {
       result = f2.1(input);
    }
   catch (e)
   {
      throw e;
    }
    f2.2(result );
}

function f2.1(in)
{
   ...
   if(errorFound)
   {
     throw new Exception();   --> EA error : Exception is undefined
    }    ...
}
Title: Re: throw / catch exceptions with JScript
Post by: Aaron B on August 07, 2019, 11:57:41 am
See:
https://www.w3schools.com/js/js_errors.asp

For JavaScript (AFAIK also applies to JScript) the syntax is like:

Code: [Select]
throw "Error message here";
If the error you are seeing is something like "Exception thrown and not caught", then it probably means that you need to add a try-catch block somewhere higher up in your script.