Skip to main content

User defined exceptions should be handled

ZPA rule for PL/SQL and Oracle SQL · UnhandledUserDefinedException

When a custom exception is declared and not handled in the code, raising this exception will cause the error "ORA-06510: PL/SQL: unhandled user-defined exception" in the database or the error "User-defined exception" in Oracle Forms.

It is a good practice to handle custom exceptions.

Examples​

Noncompliant Code Example
DECLARE
my_exception EXCEPTION;
BEGIN
...
RAISE my_exception; -- this will cause a "user-defined exception"
END;
Compliant Solution
DECLARE
my_exception EXCEPTION;
BEGIN
...
RAISE my_exception;
EXCEPTION
WHEN my_exception THEN
...
END;

This check will also trigger a violation if the exception is handled by an OTHERS handler and it has a reference to SQLERRM. In this case, the SQLERRM will return "User-defined exception", which is not very useful.

DECLARE
my_exception EXCEPTION;
BEGIN
...
RAISE my_exception;
EXCEPTION
WHEN OTHERS THEN
log(SQLERRM);
END;