SunQuest
 
       Oracle
  Home arrow Oracle arrow Page 3 - Database Interaction with PL/SQL: Pre-...
Dev Shed Forums 
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Forums Sitemap 
IBM® developerWorks 
Sun Developer Network 
Dedicated Servers 
E-Commerce Hosting 
Linux Web Hosting 
Managed Hosting 
Small Business Hosting 
Actuate Whitepapers 
VeriSign Whitepapers 
VPS Hosting 
Weekly Newsletter

 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
ORACLE

Database Interaction with PL/SQL: Pre-defined Exceptions
By: Jagadish Chatarji
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 16
    2005-06-28

    Table of Contents:
  • Database Interaction with PL/SQL: Pre-defined Exceptions
  • No Data Found, Too Many Rows
  • Dup Val on Index, Zero Divide, Invalid Number, Value Error
  • A Word About OTHERS Exception

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT

    Stay one step ahead of the competition. Evaluate and give feedback on some of the hottest web development tools on the market today. Make your opinion heard! Click Here

    Database Interaction with PL/SQL: Pre-defined Exceptions - Dup Val on Index, Zero Divide, Invalid Number, Value Error


    (Page 3 of 4 )

    DUP_VAL_ON_INDEX

    This is bit different from the above two exceptions. This exception is not at all related to SELECT..INTO statement (as above exceptions). This exception gets raised when you try to update (or even insert) a row with a duplicate value in to a column constrained by a unique index (obviously Primary key as well). Let us consider the following program.

    Declare
      v_empno emp.empno%type:=&empno;
      v_ename emp.ename%type:='&ename';
      v_deptno emp.deptno%type:=&deptno;
    Begin
      Insert into emp(empno,ename,deptno)
      Values (v_empno,v_ename,v_deptno);
      dbms_output.put_line('Employee Successfully Inserted');
    Exception
      When DUP_VAL_ON_INDEX then
        dbms_output.put_line('Employee ID already exists');
    End ;

    The flow of execution of program will be something like the following:

    • Declares the variables and accepts input from the user
    • Tries to execute the INSERT statement based on the values provided by user
    • If INSERT statement gets successfully executed, it displays a positive message to user that the row has been inserted and directly jumps to END statement (skipping the exception section).
    • If INSERT fails due to duplication in a column, constrained with unique index (or primary key), it immediately jumps to exception section (skipping the DBMS_OUTPUT statement next to INSERT statement) and checks whether the exception (or error) is DUP_VAL_ON_INDEX or not. If so, displays the message 'Employee ID already exists'.

    ZERO_DIVIDE, INVALID_NUMBER, VALUE_ERROR

    The above two predefined exceptions worked well with SELECT..INTO statement. But some predefined exceptions may not be related to SELECT..INTO statement. Let us see ZERO_DIVIDE, INVALID_NUMBER and VALUE_ERROR together within this section.

    No number can be divided with zero as it results in infinite. To handle such type of situation, there exists ZERO_DIVIDE exception. Let us consider the following example.

    Declare
      x number := &x;
      y number := &y;
      z number;
    Begin
      z := x/y;
      dbms_output.put_line ('Result: ' || z);
    Exception
      When ZERO_DIVIDE then
        dbms_output.put_line ('Division by zero occured');
    End ;

    The above program illustrates simple variable declarations of numeric data type. Here, we declare three variables (x, y and z). The values of variables 'x' and 'y' are accepted from the user. Observe that all the variables are of type number (any numeric data type is acceptable). Within the body, we divide 'x' with 'y' and store the result in 'z'.

    Everything works fine as long as you provide proper values. If you try to provide the value of 'y' as zero, then ZERO_DIVIDE exception gets raised and a message gets displayed to user.

    Coming to INVALID_NUMBER, I hope you can easily understand that whenever PL/SQL runtime is unable to convert a character string to number, the exception gets raised. The following example demonstrates the same.

    Declare
      v_empno varchar2(4):='&empno';
      v_ename varchar2(20):='&ename';
      v_deptno varchar2(2):='&deptno';
    Begin
      Insert into emp(empno,ename,deptno)
      Values (v_empno,v_ename,v_deptno);
    Exception
      When INVALID_NUMBER then
         dbms_output.put_line('Given Employee Number or deptno is Invalid');
    End ;

    Coming to VALUE_ERROR, this is almost similar to INVALID_NUMBER except that it works with almost any data type. INVALID_NUMBER gets raised only when SQL statement is issued whereas VALUE_ERROR gets raised with PL/SQL statements. Let us go through the following example.

    Declare
      z number;
    Begin
      z := '&x' + '&y';
      dbms_output.put_line ('Result: ' || z);
    Exception
      When VALUE_ERROR then
        dbms_output.put_line ('Invalid values');
    End ;

    The above program accepts the value of 'x' and 'y' in the form of character strings and implicitly converts them to numeric. If user provides a non-numeric value, the VALUE_ERROR gets raised. In this case, INVALID_NUMBER will not be raised as nothing is related with SQL statement.

    More Oracle Articles
    More By Jagadish Chatarji


       · Hello guys, this is my article on working with pre-defined exceptions in PL/SQL. You...
     

       

    ORACLE ARTICLES

    - Tuning PL/SQL Code
    - Debugging PL/SQL Code
    - Testing PL/SQL Code
    - Working With PL/SQL Code
    - Conditional Compilation for Oracle Database ...
    - Compile-Time Warnings for Oracle DB 10g
    - Compiling PL/SQL Code for an Oracle Database
    - Troubleshooting PL/SQL Code
    - Managing PL/SQL Code
    - Data Manipulation and More for HTML DB Appli...
    - Oracle Database Fundamentals
    - Adding Processes to HTML DB Applications
    - Adding Computations, Processes, and Validati...
    - Sub-templates and More with Oracle HTML DB
    - Focusing on Templates in Oracle HTML DB





    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway