Try catch in JavaScript block
Quote from Almer on 07/02/2024, 10:26Dear Support Team,
How may I use try-catch block in JavaScript Script block?
I am asking this question because when I tried to use try-catch for parsing JSON by the code:
try{
return JSON.parse(data)
}catch(error){
console.log(error)
return {}
}when the data cant be converted by JSON parser, the script raise an non-catched error and the script suspends. Could you please tell me how to manage the error?
Dear Support Team,
How may I use try-catch block in JavaScript Script block?
I am asking this question because when I tried to use try-catch for parsing JSON by the code:
try{
return JSON.parse(data)
}catch(error){
console.log(error)
return {}
}
when the data cant be converted by JSON parser, the script raise an non-catched error and the script suspends. Could you please tell me how to manage the error?
Quote from mm_dev on 07/02/2024, 10:42Your usage is correct. The issue here is that the way that JSON.parse() triggers this error explicitly invokes the debugger for some reason (just as if you had set a breakpoint). If you let the execution continue at that point you should see that your catch block is executed as intended.
You can avoid the debugger by disabling the "Debug Mode" option in the script block, but then it will not be available in general (so no breakpoints or stepping through execution).
Will have to check if there is a specific reason for this behavior.
Your usage is correct. The issue here is that the way that JSON.parse() triggers this error explicitly invokes the debugger for some reason (just as if you had set a breakpoint). If you let the execution continue at that point you should see that your catch block is executed as intended.
You can avoid the debugger by disabling the "Debug Mode" option in the script block, but then it will not be available in general (so no breakpoints or stepping through execution).
Will have to check if there is a specific reason for this behavior.
Quote from Almer on 07/02/2024, 11:41Could you please tell us if we can do something to prevent the debugger environment from opening and directly enter the catch block? Or for example, have the program continue automatically? Because we do not only encounter this problem with JSON.parse() and in many cases, using try-catch is practically not usable for us in the script block
Could you please tell us if we can do something to prevent the debugger environment from opening and directly enter the catch block? Or for example, have the program continue automatically? Because we do not only encounter this problem with JSON.parse() and in many cases, using try-catch is practically not usable for us in the script block
Quote from mm_dev on 07/02/2024, 11:57Already added that info above: Disabling "Debug Mode" will turn the debugger invocation into a no-op, but also prevent you from using the debugger functionality yourself.
I've forwarded this issue internally (exception handling in general isn't quite working as I'd expect it), so maybe we can get this changed for the next release. However as this likely would impact existing code I can't promise anything.
Already added that info above: Disabling "Debug Mode" will turn the debugger invocation into a no-op, but also prevent you from using the debugger functionality yourself.
I've forwarded this issue internally (exception handling in general isn't quite working as I'd expect it), so maybe we can get this changed for the next release. However as this likely would impact existing code I can't promise anything.