Skip to content

SQLEXCEPTION

DROP PROCEDURE IF EXISTS sampleProcedure;
DELIMITER //
CREATE PROCEDURE sampleProcedure()
BEGIN
DECLARE EXIT HANDLER FOR SQLEXCEPTION
SELECT 'An SQL exception has occurred.';
DROP TABLE nonExistentTable;
END//
DELIMITER ;
CALL sampleProcedure();
'An SQL exception has occurred.'

In the example, a stored procedure named sampleProcedure is created. Inside this procedure, an exit handler for SQLEXCEPTION is declared. An SQL statement that will definitely result in an exception (dropping a non-existent table) is issued. When the procedure is called and the exception is raised, the exit handler is invoked and the message ‘An SQL exception has occurred.’ is displayed.