Oracle Plsql catching exceptions and throwing exceptions

Source: Internet
Author: User
Tags throw exception

While writing Oracle stored procedures, many things put into stored procedures, such as some judgments, are much simpler than in the program logic, but also involve capturing and throwing the same problem.

Catch Exception Syntax:

EXCEPTION when    excepttion_name1 then      ...    . When the excepttion_name2 then     ...    When the Excepttion_name3 then     ... End;
Example:
Declare     a  int:=0;    b int:=1;   Ex_1 exception;   Ex_2 exception;  Begin     If A=0 then    raise ex_1;    End If;        If B=1 then       raise ex_2;    End If;        Exception when      ex_1         then Dbms_output.put_line (' caught error 1 ');      When Ex_2          then Dbms_output.put_line (' caught error 2 '); end;

Output:
Error 1 was caught

Because of the error in Ex_1 place, the following ex_2 is not executed, but instead jumps directly to the error-handling section of the code. An exception is not allowed in Oracle to be handled by multiple exception handling blocks.

Use Ohters to handle all errors
Declare     a  int:=0;    Ex_1 exception; Begin     If A=0 then    raise ex_1;    End If;         Exception when       others and        then Dbms_output.put_line (' Catch a global error ');  End


Output:

Throw Exception Raise_application_error function

The function is to transfer application-specific errors from the server side to the client application (Sqlplus on other machines or the foreground development language)

PROCEDURE raise_application_error (error_number_in in number, error_msg_in in VARCHAR2);    ERROR_NUMBER_IN: Custom error code, allowable from 20000 to 20999, so that there is no conflict with any error code of ORACLE.    error_msg_in: Length cannot exceed 2k, otherwise intercept 2k

Example: let a number not be 0

Declare   a int:=0;begin    if a=0 then       raise_application_error (-20001, ' value cannot be 0 ');    End If; End


Run:

Oracle Plsql catching exceptions and throwing exceptions

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.