liveBook · Manning

Exception block

As explained in Snowflake Data Engineering

An exception block is a Snowflake Scripting block that specifies statements to execute when an exception occurs.

An exception block is a Snowflake Scripting construct that handles errors in stored procedures. Normal execution skips it; execution resumes there when an error occurs. section 4.3.3

Use an exception block to:

  • Reverse a transaction or perform other remediation actions after failure. section 4.3.3
  • Return a diagnostic message containing SQLERRM, the error message stored by Snowflake. section 4.3.3
  • Route different failures through WHEN clauses for built-in or custom exceptions; WHEN OTHER handles remaining exceptions. section 4.3.3
Listing 4.9 Exception handling in a stored procedure
use database BAKERY_DB;
use schema TRANSFORM;
create or replace procedure LOAD_CUSTOMER_ORDERS()
returns varchar
language sql
as
$$
begin
  merge into CUSTOMER_ORDERS_COMBINED tgt...            #1
  return 'Load completed. ' || SQLROWCOUNT || ' rows affected.';
exception                                                     #2
  when other then
    return 'Load failed with error message: ' || SQLERRM;
end;
$$
;

A stored procedure wraps a merge and success return with an EXCEPTION block that catches all other failures and returns SQLERRM. section 4.3.3

How Snowflake Data Engineering covers it

How deep it goes
Explains control flow, WHEN dispatch, custom exceptions, transaction reversal, and SQLERRM, but defers detailed exception definitions.
Examples
Demonstrates LOAD_CUSTOMER_ORDERS with WHEN OTHER, SQLERRM, SQLROWCOUNT, and a merge operation.
What it assumes
Assumes familiarity with stored procedures and Snowflake Scripting while introducing exception handling through an executable SQL procedure.
What you can do after
Apply it via Snowflake Scripting to catch procedure failures and return remediation results or error messages.
How it connects
Connects exception handling with stored procedures, transactions, custom exceptions, SQLERRM, and data-pipeline resilience.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of Snowflake Data Engineering. The section links point to the parts of the book it draws on.