SQL Keywords

SQL SQLEXCEPTION

What is SQLEXCEPTION in SQL?

SQLEXCEPTION is a generic condition label used in DECLARE HANDLER statements to trap any SQL runtime error in MySQL-style stored programs.
Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.

Compatible dialects for SQL SQLEXCEPTION:

SQL SQLEXCEPTION Full Explanation

SQLEXCEPTION is one of three predefined condition names (SQLWARNING, NOT FOUND, SQLEXCEPTION) that you can reference when declaring an error handler inside a MySQL or MariaDB stored procedure, function, trigger, or event. Whereas SQLWARNING captures warnings (SQLSTATE classes starting with '01') and NOT FOUND captures the no-data condition (SQLSTATE '02000'), SQLEXCEPTION matches every other non-successful SQLSTATE—that is, any error that would normally cause statement failure. A handler declared for SQLEXCEPTION fires when the first statement that follows the DECLARE section raises an error. Depending on the handler type (EXIT, CONTINUE, or UNDO), control either leaves the stored routine, continues after the failing statement, or rolls back work done in the compound statement (UNDO is only supported in DB2). Typical uses include rolling back a transaction, logging the error information, or converting the error to a custom SQLSTATE via SIGNAL. SQLEXCEPTION works only inside compound statements (BEGIN … END) of stored programs and cannot be used in plain SQL scripts executed outside those contexts.

SQL SQLEXCEPTION Syntax

DECLARE {EXIT | CONTINUE} HANDLER
    FOR SQLEXCEPTION
BEGIN
    -- error handling logic
END;

SQL SQLEXCEPTION Parameters

Example Queries Using SQL SQLEXCEPTION

DELIMITER $$
CREATE PROCEDURE transfer_funds(p_from INT, p_to INT, p_amt DECIMAL(10,2))
BEGIN
    DECLARE EXIT HANDLER FOR SQLEXCEPTION
    BEGIN
        ROLLBACK;
        SIGNAL SQLSTATE '45000'
            SET MESSAGE_TEXT = 'Transfer failed';
    END;

    START TRANSACTION;
    UPDATE accounts SET balance = balance - p_amt WHERE id = p_from;
    UPDATE accounts SET balance = balance + p_amt WHERE id = p_to;
    COMMIT;
END$$
DELIMITER ;

Expected Output Using SQL SQLEXCEPTION

  • If any statement inside the procedure raises an error (for example, insufficient balance or invalid account), the SQLEXCEPTION handler runs, rolls back the transaction, raises a custom error, and the caller receives SQLSTATE 45000 with message 'Transfer failed'

Use Cases with SQL SQLEXCEPTION

  • Implementing transaction-safe stored procedures that must roll back on any error
  • Logging or auditing failed DML inside triggers
  • Converting internal errors to business-specific messages with SIGNAL
  • Preventing partial data changes by exiting immediately on failure

Common Mistakes with SQL SQLEXCEPTION

  • Forgetting to place DECLARE HANDLER before the first executable statement
  • Expecting SQLEXCEPTION to work in plain SQL scripts outside stored programs
  • Using CONTINUE when the logic really needs EXIT, leading to phantom commits
  • Handling SQLEXCEPTION but omitting ROLLBACK, which can leave the transaction open
  • Confusing SQLEXCEPTION with SQLWARNING and NOT FOUND

Related Topics

First Introduced In

MySQL 5.0 (initial stored procedure support)

Frequently Asked Questions

How does SQLEXCEPTION differ from SQLWARNING and NOT FOUND?

SQLWARNING handles SQLSTATE codes starting with '01' (warnings), NOT FOUND handles '02000' (no data), and SQLEXCEPTION catches all other error codes, making it a general error handler.

Does SQLEXCEPTION roll back the current transaction automatically?

No. The handler merely intercepts the error. You must issue ROLLBACK explicitly inside the handler or rely on autocommit to undo changes.

Can I declare multiple handlers including SQLEXCEPTION?

Yes. You can have specific SQLSTATE or condition handlers first and add a SQLEXCEPTION handler as a fallback. The most specific matching handler is executed.

Is SQLEXCEPTION supported outside stored programs?

No. SQLEXCEPTION (and DECLARE HANDLER in general) can only be used inside stored procedures, functions, triggers, or events.

Sign up to get up to date news on SQL keywords
Welcome to the Galaxy, Guardian!
You'll be receiving a confirmation email

Follow us on twitter :)
Oops! Something went wrong while submitting the form.
Trusted by top engineers on high-velocity teams
Aryeo Logo
Assort Health
Curri
Rubie Logo
Bauhealth Logo
Truvideo Logo

Check out other commonly used SQL Keywords!