3

I have the following code:

<script runat="server">

Platform.Load("Core","1.1.1");

try{
    var logDE = DataExtension.Init("testDE");

    throw "this is an error";

    logDE.Rows.Add({"c1":"success"});
} 

catch(e)
{
    logDE.Rows.Add({"c1":e});
}
</script>

I run this inside Automation Studio. However, it should throw an error and use the catch block.

The catch block will add a row to the DE "testDE" with "this is an error".

This is my theory, but I just get an error during execution without any information of why.

To rule out any issues with the DE I have removed the throw and the DE is populating with "success".

Why is my catch block not working?

4
  • @Sascha--> if any error rise mean that time only came to catch block otherwise not
    – Ravi.A
    May 27, 2019 at 5:47
  • of course so why is throw "this is an error"; not throwing the error?
    – Sascha
    May 27, 2019 at 7:45
  • Can you confirm that your code in the try block is being called? Are you getting a success log? May 27, 2019 at 14:42
  • Hi Nishant, yes as said in my question: "To rule out any issues with the DE I have removed the throw and the DE is populating with "success"."
    – Sascha
    May 27, 2019 at 22:07

1 Answer 1

3

Variables declared in the try block context cannot be accessed in the catch block. So even though the exception is caught, there is an error in the catch block.

try {
    var a = 1;
    throw 'myException';
} catch(e) {
    Platform.Response.Write(a);   
    // exception is caught and output is "undefined" 
}

You can either init your DE again inside the catch block or move the variable declaration outside the try block.

var logDE = DataExtension.Init("testDE");
try{
    throw "this is an error";
    logDE.Rows.Add({"c1":"success"});
} catch(e) {
    logDE.Rows.Add({"c1":e});
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.