I Need to a throw and display the catch errors if the condition meets

Maradani Revat1
Tera Contributor

Hi,

I had a requirement to add a throw if the app name is null and in throw i need to display catch message. can anyone pls help me how to write the throw block and display the error message from catch block. Below is the script.

try {
    var recentAppJson = {};
    var appName = '';
    
   recentAppJson.success = true;
} catch (ex) {
    recentAppJson.error = ex.stack;
    recentAppJson.success = false;
}

1 ACCEPTED SOLUTION

Hi

basically not, but it is always the best approach to encapsulate business logic in functions (located in Script Includes).

Maik

View solution in original post

3 REPLIES 3

Maik Skoddow
Tera Patron
Tera Patron

Hi

It seems you are not familiar with the concept of exception handling. When someone ask you to throw an exception then you should implement something like this:

function foo(strAppName) {
    if (typeof strAppName != "string" || strAppName.length == 0) {
        throw new IllegalArgumentException(
            "Expected a non-empty 'string' argument."
        );
    }
}

So the caller of the function foo() is responsible to catch the exception:

try {
  foo();
}
catch (e) {
  gs.info(e.toString);
}

Maik

Thanks Maik!!

Do i need to add to add function compulsory. Can you please suggest.

 

Regards,

Revathi

Hi

basically not, but it is always the best approach to encapsulate business logic in functions (located in Script Includes).

Maik