← PL/SQL Topics

Exception Handling

Explain SQLCODE and SQLERRM in PL/SQL exception handlers

SQLCODE and SQLERRM

Oracle Fusion / EBS · Technical · PL/SQL

GeneralHigh confidence

SQLCODE returns the numeric error of the exception being handled; SQLERRM returns its message text.

Grounded in: Curated Oracle knowledge layer — PL/SQL

How it works

Inside an exception handler — most often a generic WHEN OTHERS — SQLCODE returns the numeric error code of the exception currently being handled (0 if none, 1 for a user-defined exception raised without a code), and SQLERRM returns the associated error message text. Together they're what you log or re-raise with when a generic OTHERS handler needs to report specifically what went wrong instead of silently swallowing it.

How it works
BEGIN
  UPDATE employees SET salary = salary / 0 WHERE employee_id = 100;
EXCEPTION
  WHEN OTHERS THEN
    DBMS_OUTPUT.PUT_LINE('Error code: ' || SQLCODE);
    DBMS_OUTPUT.PUT_LINE('Error message: ' || SQLERRM);
    RAISE; -- re-raise after logging, don't swallow it
END;
/

Related Questions

Have a follow-up, or a different question?

Continue in Ask Oracle AI