- The return keyword returns a value and exits the current function. The returned value may be an error message, but it's returned as a regular string not an error condition.
- The throw keyword raises an error and exits the current function. If nothing in the execution stack above it catches the error, execution stops with an error.
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.