Propagation of Exceptions between Program Units : User Defined Exceptions « PL SQL Programming « Oracle PL/SQL Tutorial

Oracle PL/SQL Tutorial
1. Introduction
2. Query Select
3. Set
4. Insert Update Delete
5. Sequences
6. Table
7. Table Joins
8. View
9. Index
10. SQL Data Types
11. Character String Functions
12. Aggregate Functions
13. Date Timestamp Functions
14. Numerical Math Functions
15. Conversion Functions
16. Analytical Functions
17. Miscellaneous Functions
18. Regular Expressions Functions
19. Statistical Functions
20. Linear Regression Functions
21. PL SQL Data Types
22. PL SQL Statements
23. PL SQL Operators
24. PL SQL Programming
25. Cursor
26. Collections
27. Function Procedure Packages
28. Trigger
29. SQL PLUS Session Environment
30. System Tables Data Dictionary
31. System Packages
32. Object Oriented
33. XML
34. Large Objects
35. Transaction
36. User Privilege
Java
Java Tutorial
Java Source Code / Java Documentation
Java Open Source
Jar File Download
Java Articles
Java Products
Java by API
Photoshop Tutorials
Maya Tutorials
Flash Tutorials
3ds-Max Tutorials
Illustrator Tutorials
GIMP Tutorials
C# / C Sharp
C# / CSharp Tutorial
C# / CSharp Open Source
ASP.Net
ASP.NET Tutorial
JavaScript DHTML
JavaScript Tutorial
JavaScript Reference
HTML / CSS
HTML CSS Reference
C / ANSI-C
C Tutorial
C++
C++ Tutorial
Ruby
PHP
Python
Python Tutorial
Python Open Source
SQL Server / T-SQL
SQL Server / T-SQL Tutorial
Oracle PL / SQL
PostgreSQL
SQL / MySQL
MySQL Tutorial
VB.Net
VB.Net Tutorial
Flash / Flex / ActionScript
VBA / Excel / Access / Word
XML
XML Tutorial
Microsoft Office PowerPoint 2007 Tutorial
Microsoft Office Excel 2007 Tutorial
Microsoft Office Word 2007 Tutorial
Oracle PL/SQL Tutorial » PL SQL Programming » User Defined Exceptions 
24. 21. 4. Propagation of Exceptions between Program Units
SQL>
SQL> create or replace function f_makeAddress (i_address VARCHAR2,i_city VARCHAR2,i_state VARCHAR2,i_zip VARCHAR2)
  2  return VARCHAR2
  3  is
  4     e_badZip EXCEPTION;
  5     pragma EXCEPTION_init(e_badZip,-20998);
  6     v_out VARCHAR2(256);
  7  begin
  8     p_validateZip (i_zip);
  9     v_out:= i_address||', '||i_city ||', '||i_state ||', '||i_zip;
 10     return v_out;
 11  exception
 12     when e_badZip then
 13       return i_zip || ': Invalid zip code.';
 14  end;
 15  /

Function created.

SQL>
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;
  8  Begin
  9     if length(i_zipCode)then
 10       Raise e_tooShort;
 11     elsif  length(i_zipCode)then
 12       Raise e_tooLong;
 13     end if;
 14
 15     v_tempZip := to_number(i_zipCode);
 16
 17  exception
 18     when e_tooLong then
 19        DBMS_OUTPUT.put_line('long zip');
 20        raise e_badZip;
 21     when e_tooShort then
 22        DBMS_OUTPUT.put_line('short zip');
 23        -- raise e_badZip SHOULD be here
 24     when value_error then
 25        DBMS_OUTPUT.put_line('non-numeric zip');
 26        raise; -- re-raising the same exception
 27  end;
 28  /

Procedure created.

SQL> --Scenario 1: No rule violations
SQL>
SQL> declare
  2      v_out VARCHAR2(2000);
  3  begin
  4     v_out:=f_makeAddress('AA','City','CA','94061');
  5     DBMS_OUTPUT.put_line(v_out);
  6  end;
  7  /
AA, City, CA, 94061

PL/SQL procedure successfully completed.

SQL>
SQL>
SQL> --Scenario 2: Short ZIP code
SQL>
SQL>
SQL> declare
  2        v_out VARCHAR2(2000);
  3  begin
  4       v_out:=f_makeAddress('A','City', 'CA','940');
  5        DBMS_OUTPUT.put_line(v_out);
  6  end;
  7  /
short zip
A, City, CA, 940

PL/SQL procedure successfully completed.

SQL> --Scenario 3: Non-numeric ZIP code
SQL>
SQL> declare
  2      v_out VARCHAR2(2000);
  3  begin
  4     v_out:=f_makeAddress('A','City' , 'CA','9406A');
  5     DBMS_OUTPUT.put_line(v_out);
  6  end;
  7  /
non-numeric zip
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character to number conversion error
ORA-06512: at "JAVA2S.P_VALIDATEZIP", line 26
ORA-06512: at "JAVA2S.F_MAKEADDRESS", line 8
ORA-06512: at line 4


SQL> -- Long ZIP code
SQL>
SQL> declare
  2      v_out VARCHAR2(2000);
  3  begin
  4      v_out:=f_makeAddress('A','City','CA','940612345');
  5      DBMS_OUTPUT.put_line(v_out);
  6  end;
  7  /
long zip
940612345: Invalid zip code.

PL/SQL procedure successfully completed.

SQL>
24. 21. User Defined Exceptions
24. 21. 1. Adding User-Defined Exceptions
24. 21. 2. A User-Defined Exception
24. 21. 3. Including error messages in user-defined exceptions
24. 21. 4. Propagation of Exceptions between Program Units
www.java2java.com | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.