Reputation: 180
I have a question regarding the exception handling in snowflake. I understand that we can use try & catch block to handle the error and use return/throw the error message.
Upvotes: 0
Views: 3475
Reputation: 11046
Throw will either generate a new error or send the existing error up the stack. If you use a try/catch block and "return" information on the error, the function you're leaving will not indicate an error condition. It may send information on the error, but since the code caught the error it will not go up the stack and potentially terminate execution if nothing up the stack catches it.
When you're running a JavaScript UDF or stored procedure from the Snowflake UI, you can tell there's been an uncaught error because the return comes back in red. If the error comes back in black or blue, even if the error message reports an error, it's been caught and the status of the call is not an error. Here's an example:
create or replace procedure FOO()
returns string
language javascript
as
$$
// Run a helper function here
myFunction();
function myFunction(){
var mySqlStatement = "select * from NONEXISTENT_TABLE";
var statement1 = snowflake.createStatement({sqlText: mySqlStatement});
statement1.execute();
}
$$;
call foo(); // This will generate an error and return in red text.
create or replace procedure FOO()
returns string
language javascript
as
$$
try{
myFunction();
}
catch(e){
return e.message;
}
function myFunction(){
var mySqlStatement = "select * from NONEXISTENT_TABLE";
var statement1 = snowflake.createStatement({sqlText: mySqlStatement});
statement1.execute();
}
$$;
call foo();
// This will catch an error and return it in blue text.
//External clients will not report an error, even through the
//SP returns the error message.
Upvotes: 2