EXCEPTION (Snowflake Scripting)¶
指定如何处理 Snowflake Scripting 块中引发的异常。
For more information on exceptions, see Handling exceptions.
- See also:
语法
其中:
exception_nameAn exception name defined in the DECLARE portion of the current block, or in an enclosing block.
使用说明
-
Each block can have its own exception handler.
-
Snowflake supports no more than one exception handler per block. However, that handler can catch more than one type of exception by having more than one
WHENclause. -
The
WHEN OTHER [ { EXIT | CONTINUE } ] THENclause catches any exception not yet specified. -
An exception handler applies to statements between the BEGIN and EXCEPTION sections of the block in which it is declared. It does’t apply to the DECLARE section of the block.
-
An exception handler can handle a specified exception only if that specified exception is in scope.
-
If a stored procedure is intended to return a value, then it should return a value from each possible exit path, including each
WHENclause ofEXITtype in the exception handler. -
To use a variable in an exception handler, the variable must be declared in the DECLARE section or passed as an argument to a stored procedure. It can’t be declared in the BEGIN … END section. For more information, see Passing variables to an exception handler in Snowflake Scripting.
-
When an exception occurs, the handler conditions are checked in order and the first
WHENclause that matches is used. The order within a block is top to bottom, and the inner blocks are checked before the outer blocks. There is no preference in matchingEXITorCONTINUEhandlers, whichever matches first is used. -
Only one handler can be matched for a statement. However, any exceptions encountered inside of an exception handler body can trigger outer block exception handlers.
-
Each
WHENclause in an exception handler can be one of the following types:-
EXIT- The block runs the statements in the handler and then exits the current block. If the block runs an exception of this type, and the block contains statements after the exception handler, those statements aren’t run.If the block is an inner block, and the exception handler doesn’t contain a
RETURNstatement, then execution exits the inner block and continues with the code in the outer block.EXITis the default. -
CONTINUE- The block executes the statements in the handler and continues with the statement immediately following the one that caused the error.
An
EXCEPTIONclause can haveWHENclauses of both types —EXITandCONTINUE.For a
WHENclause of theCONTINUEtype, the following usage notes apply:- If an error is raised in a branching construct, then the continuing statement is the statement immediately after the branching construct.
- If an error is raised in the condition of a loop, then the continuing statement is the statement immediately after the loop.
- If an error is raised in the body of a loop, then the continuing statement is the statement in the next iteration of the loop. For an example, see 处理异常并继续.
- If an error is raised in a RETURN statement, then the
continuing statement is the statement immediately after the
RETURNstatement. - If an error is raised in a nested stored procedure and the error is handled by the outer scope, then the continuing statement is the statement immediately after the stored procedure call.
- Avoid including a
RETURNstatement in aWHENclause of theCONTINUEtype. If you include aRETURNstatement, then the stored procedure returns without continuing.
For a
WHENclause of theCONTINUEtype, the following examples show which statement is the statement immediately following the one that caused the error for different scenarios. In these examples, theerror_expressionis the expression that raised the exception, and thecontinue_statementis the statement that the code continues with in the block after theCONTINUEhandler statements. -
示例
以下示例展示了如何声明和触发异常,以及如何使用异常处理程序处理这些异常:
处理多种类型的异常
以下示例展示了一个设计用于处理多种类型异常的异常处理程序:
Note: If you use Snowflake CLI, SnowSQL, the Classic Console, or the
execute_stream or execute_string method in Python Connector
code, use this example instead (see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector):
该输出表明异常处理程序已捕获异常:
处理异常并继续
The following example shows an exception handler with a WHEN clause of the CONTINUE type:
Note: If you use Snowflake CLI, SnowSQL, the Classic Console, or the
execute_stream or execute_string method in Python Connector
code, use this example instead (see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector):
The output shows that the exception handler caught the exception, executed a statement that added
1 to the counter, and then executed the next statement after the exception was caught, which
added 10 to the counter:
The following example shows how an exception handler with a WHEN clause of the CONTINUE type works
when an error is raised in a loop. The example raises an error on the first iteration because it tries to
divide the value 10 by zero. The CONTINUE handler logs the error in the error_log_table, and the block
continues with the next iteration of the loop, which divides 10 by 1. The loop continues to iterate until
10 is divided by 5 and the loop ends. The output is 2:
Note: If you use Snowflake CLI, SnowSQL, the Classic Console, or the
execute_stream or execute_string method in Python Connector
code, use this example instead (see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector):
在嵌套块中处理异常
以下示例展示了嵌套块,并说明内部块可以引发在内部块或外部块中声明的异常:
Note: If you use Snowflake CLI, SnowSQL, the Classic Console, or the
execute_stream or execute_string method in Python Connector
code, use this example instead (see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector):
该输出表明异常处理程序已捕获异常:
以下示例与上一个示例类似,但演示了嵌套块,每个块都有自己的异常处理程序。
Note: If you use Snowflake CLI, SnowSQL, the Classic Console, or the
execute_stream or execute_string method in Python Connector
code, use this example instead (see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector):
Note
This example uses the same exception name (e1) in the outer and inner blocks, which isn’t recommended.
The example does this to illustrate the scope of exception names. The two exceptions with the
name e1 are different exceptions.
The e1 handler in the outer block doesn’t handle the exception e1 that is declared and raised in the inner block.
该输出表明内部异常处理程序已运行:
处理同一子句中的多种异常及未指定异常
以下示例片段展示了如何执行两项任务:
- Catch more than one exception in the same clause by using
OR. - Catch unspecified exceptions by using
WHEN OTHER THEN.
使用内置变量处理异常
The following example shows how to return SQLCODE, SQLERRM (SQL error message), and SQLSTATE built-in variable values when catching an exception:
Note: If you use Snowflake CLI, SnowSQL, the Classic Console, or the
execute_stream or execute_string method in Python Connector
code, use this example instead (see Using Snowflake Scripting in Snowflake CLI, SnowSQL, and Python Connector):
运行此示例将生成以下输出: