How to catch the null constraint exception in plsql when inserting a record?

I want to check the null constraint of a column when inserting a record into a table. There, I am trying to insert a null value through a procedure to a column which cannot be null. Here is my procedure.

CREATE OR REPLACE PROCEDURE RecordInsert(s_id IN NUMBER, pid IN NUMBER, pr_id IN NUMBER, quantity NUMBER, rv DATE, exercise_num NUMBER)
IS
null_constraint  EXCEPTION;
PRAGMA EXCEPTION_INIT(null_constraint,-1451);
BEGIN
  INSERT INTO Supplier_Part_Project_Tab
  VALUES(s_id, pid, pr_id_, quantity, rv);
  dbms_output.put_line('The row successfully inserted');
  COMMIT;

EXCEPTION
  WHEN null_constraint THEN
    dbms_output.put_line('The column cannot be NULL');
  WHEN OTHERS THEN
      pkg_Error.prc_Exeception(exercise_num);
END;

This is how I executed the procedure.

BEGIN
 RecordInsert( 1002, '', 2001, 80, (TO_DATE('2003/05/03 09:02:44', 'yyyy/mm/dd hh12:mi:ss AM')),8);
COMMIT;
END;

In the above procedure, I am trying to insert pid as null. There, I want to fire the 'null_constraint' exception. But it fires the 'OTHERS' exception. Hope the oracle exception -1451 is correct for my requirement. But why does it not fire?


Sample table:

SQL> CREATE TABLE test
  2  (
  3     id     NUMBER NOT NULL,
  4     name   VARCHAR2 (20) NOT NULL
  5  );

Table created.

Procedure without exception handling section (to see what will happen):

SQL> CREATE OR REPLACE PROCEDURE RecordInsert (par_id    IN NUMBER,
  2                                            par_name  IN VARCHAR2)
  3  IS
  4     null_constraint  EXCEPTION;
  5     PRAGMA EXCEPTION_INIT (null_constraint, -1451);
  6  BEGIN
  7     INSERT INTO test (id, name)
  8          VALUES (par_id, par_name);
  9
 10     DBMS_OUTPUT.put_line ('The row successfully inserted');
 11  /*
 12  EXCEPTION
 13     WHEN null_constraint
 14     THEN
 15        DBMS_OUTPUT.put_line ('The column cannot be NULL');
 16     WHEN OTHERS
 17     THEN
 18        DBMS_OUTPUT.put_line ('Others');
 19  */
 20  END;
 21  /

Procedure created.

Testing:

SQL> exec recordinsert(1, 'Littlefoot');
The row successfully inserted

PL/SQL procedure successfully completed.

SQL> exec recordinsert(2, null);
BEGIN recordinsert(2, null); END;

*
ERROR at line 1:
ORA-01400: cannot insert NULL into ("SCOTT"."TEST"."NAME")
ORA-06512: at "SCOTT.RECORDINSERT", line 7
ORA-06512: at line 1


SQL>

Aha. ORA-01400 (not 1451). Let's modify the procedure, then:

SQL> CREATE OR REPLACE PROCEDURE RecordInsert (par_id    IN NUMBER,
  2                                            par_name  IN VARCHAR2)
  3  IS
  4     null_constraint  EXCEPTION;
  5     PRAGMA EXCEPTION_INIT (null_constraint, -1400);
  6  BEGIN
  7     INSERT INTO test (id, name)
  8          VALUES (par_id, par_name);
  9
 10     DBMS_OUTPUT.put_line ('The row successfully inserted');
 11  EXCEPTION
 12     WHEN null_constraint
 13     THEN
 14        DBMS_OUTPUT.put_line ('The column cannot be NULL');
 15     WHEN OTHERS
 16     THEN
 17        DBMS_OUTPUT.put_line ('Others');
 18  END;
 19  /

Procedure created.

SQL> set serveroutput on
SQL>
SQL> exec recordinsert(2, null);
The column cannot be NULL                  --> here's your exception

PL/SQL procedure successfully completed.

SQL>

Error code you used was

ORA-01451: column to be modified to NULL cannot be modified to NULL

Cause: The column may already allow NULL values, the NOT NULL constraint is part of a primary key or check constraint, or an ALTER TABLE MODIFY statement attempted to change a column specification unnecessarily, from NULL to NULL.

Action: If a primary key or check constraint is enforcing the NOT NULL constraint, then drop that constraint.

It is related to ALTER TABLE statement, not to inserts which try to insert NULL values into NOT NULL columns, e.g.

SQL> create table test (id number not null, name varchar2(20));

Table created.

SQL> desc test
 Name                                      Null?    Type
 ----------------------------------------- -------- ----------------------------
 ID                                        NOT NULL NUMBER
 NAME                                               VARCHAR2(20)

SQL> alter table test modify name null;
alter table test modify name null
                        *
ERROR at line 1:
ORA-01451: column to be modified to NULL cannot be modified to NULL


SQL>