CRITICAL
Rule Definition
An empty catch block defeats the purpose of exceptions.
When an exception occurs, nothing happens and the program fails for an unknown reason. The application can be in an unknown state that will affect subsequent processing.
Since the reason for the issue (the exception type and potential embedded message) are ignored, it will require more time to fix the issue.
Remediation
The exception must be handled correctly according to its type.
Violation Code Sample
Microsoft SQL Server:
BEGIN TRY
-- Generate divide-by-zero error.
SELECT 1/0
END TRY
BEGIN CATCH
-- Non-compliant
END CATCH;
MySQL and MariaDB:
DECLARE CONTINUE HANDLER FOR SQLWARNING
# Non-compliant
BEGIN
# empty block
END;
PostgreSQL:
BEGIN
INSERT INTO db(a,b) VALUES (key, data);
RETURN;
EXCEPTION WHEN unique_violation THEN
-- Do nothing, and loop to try the UPDATE again.
NULL;
END;
Oracle:
DECLARE
pe_ratio NUMBER(3,1);
BEGIN
BEGIN ---------- sub-block begins
SELECT 1 / NVL(null, 0) INTO pe_ratio FROM dual;
EXCEPTION
WHEN ZERO_DIVIDE THEN
-- Compliant
pe_ratio := 0;
END; ---------- sub-block ends
EXCEPTION
WHEN OTHERS THEN
-- Non-Compliant
NULL;
END;
Fixed Code Sample
Microsoft SQL Server:
BEGIN TRY
-- Generate divide-by-zero error.
SELECT 1/0
END TRY
BEGIN CATCH
-- Compliant
SELECT ERROR_NUMBER() AS ErrorNumber, ERROR_MESSAGE() AS ErrorMessage;
END CATCH;
MySQL and MariaDB:
DECLARE CONTINUE HANDLER FOR SQLWARNING
# Compliant
BEGIN
# not an empty block
SET done = TRUE;
END;
PostgreSQL:
BEGIN
INSERT INTO db(a,b) VALUES (key, data);
RETURN;
EXCEPTION WHEN unique_violation THEN
RAISE NOTICE 'caught unique_violation ';
RETURN;
END;
Oracle:
DECLARE
pe_ratio NUMBER(3,1);
BEGIN
BEGIN ---------- sub-block begins
SELECT 1 / NVL(null, 0) INTO pe_ratio FROM dual;
EXCEPTION
WHEN ZERO_DIVIDE THEN
-- Compliant
pe_ratio := 0;
END; ---------- sub-block ends
EXCEPTION
WHEN OTHERS THEN
NULL;
-- Compliant
pe_ratio := Null;
END;
Reference
An insider's guide to writing robust, understandable, maintainable, state-of-the-art ABAP programs - Part 3
Andreas Blumenthal - Horst Keller
http://www.javapractices.com/Topic16.cjp
http://cwe.mitre.org/data/definitions/391.html
Related Technologies
Technical Criterion
CWE-391 - Unchecked Error Condition
About CAST Appmarq
CAST Appmarq is by far the biggest repository of data about real IT systems. It's built on thousands of analyzed applications, made of 35 different technologies, by over 300 business organizations across major verticals. It provides IT Leaders with factual key analytics to let them know if their applications are on track.