简体   繁体   中英

Snowflake Stored Procedure Exception & Failure

Experts,

Have 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.

  1. What is the different between throw and return - i tried both and it behave the same.
  2. Both of the above send back the error message. However not failing in the calling program. Do we have something similar to RAISE Error in oracle.

Please let me know if questions.

  1. 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.
  2. 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM