Using SQLCODE and SQLERRM : Raise Exception « PL SQL Programming « Oracle PL/SQL Tutorial






SQL> create table product(
  2     product_id number(4)     not null,
  3     product_description varchar2(20) not null
  4  );

Table created.

SQL>
SQL> insert into product values (1,'Java');

1 row created.

SQL> insert into product values (2,'Oracle');

1 row created.

SQL> insert into product values (3,'C#');

1 row created.

SQL> insert into product values (4,'Javascript');

1 row created.

SQL> insert into product values (5,'Python');

1 row created.

SQL>
SQL>
SQL> DECLARE
  2    v_descr VARCHAR2(20);
  3  BEGIN
  4    SELECT product_description
  5    INTO v_descr
  6    FROM product
  7    WHERE product_id =10;
  8    dbms_output.put_line(v_descr);
  9  EXCEPTION
 10    WHEN NO_DATA_FOUND THEN
 11    dbms_output.put_line('ERR:Invalid Hierarchy Code 10');
 12    WHEN OTHERS THEN
 13      dbms_output.put_line('ERR:An error occurred with info :'||
 14      TO_CHAR(SQLCODE)||' '||SQLERRM);
 15  END;
 16  /
ERR:Invalid Hierarchy Code 10

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> drop table product;

Table dropped.

SQL>








24.16.Raise Exception
24.16.1.User-Defined Errors
24.16.2.Raise Exception in a function
24.16.3.Raising an Exception Local PL/SQL Block
24.16.4.Avoiding exceptions raised in declaration part and exception handler
24.16.5.Raising an Exception in the Declaration Section
24.16.6.Exceptions Raised in the Exception Handler
24.16.7.Raising NO_DATA_FOUND
24.16.8.Raising a custom exception
24.16.9.Using WHEN OTHERS clause
24.16.10.Using SQLCODE and SQLERRM
24.16.11.Propogating a Server-side Customized Error Number and Error Message to client program using PRAGMA EXCEPTION_INIT