Avoiding exceptions raised in declaration part and exception handler : Raise Exception « PL SQL Programming « Oracle PL/SQL Tutorial






SQL> create or replace procedure p_validatezip (i_zipCode VARCHAR2)
  2  is
  3       e_tooShort EXCEPTION;
  4       e_tooLong  EXCEPTION;
  5       e_badZip   EXCEPTION;
  6       pragma exception_init(e_badZip, -20998);
  7       v_tempZip number:=to_number(i_zipCode);
  8  begin
  9       if length(i_zipCode)< 5 then
 10         raise e_tooShort;
 11       elsif  length(i_zipCode)> 6 then
 12         raise e_tooLong;
 13       end if;
 14  exception
 15       when e_tooLong then
 16          DBMS_OUTPUT.put_line('long zip');
 17          raise e_badZip;
 18       when e_tooShort then
 19          DBMS_OUTPUT.put_line('short zip');
 20       when VALUE_ERROR then
 21          DBMS_OUTPUT.put_line('non-numeric zip');
 22          raise e_badZip;
 23    end;
 24  /

Procedure created.

SQL>
SQL> declare
  2
  3  begin
  4      p_validatezip('9123412341234');
  5  end;
  6  /
long zip
declare
*
ERROR at line 1:
ORA-20998:
ORA-06512: at "JAVA2S.P_VALIDATEZIP", line 17
ORA-06510: PL/SQL: unhandled user-defined exception
ORA-06512: at line 4


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