Pl/sql Exception Handling

Source: Internet
Author: User
Tags define define exception error code exception handling execution sqlplus



When designing a Pl/sql program, this or that error occurs frequently, and exception handling is the segment of the program that handles the error, and the exception handling in Oracle 9i is divided into system predefined exception handling and custom exception handling.

System predefined exception handling

System predefined exception handling is a procedure to deal with the problems that occur during the compilation and execution of Pl/sql programs. The following code is the correct code and can be executed successfully in "Sqlplus Worksheet".
―――――――――――――――――――――――――――――――――――――
Set Serveroutput on
Declare
Tempno integer:=90;
Begin
tempno:=tempno+1;
End
―――――――――――――――――――――――――――――――――――――
"Matching program Location": 9th Chapter \ Correctplsql.sql.
The following code is an error code, and the results of the execution in "Sqlplus worksheet" are shown in Figure 9.56.

"Matching program Location": 9th Chapter \ Wrongplsql.sql.
Because of the error in the code, the system's predefined exception handling is activated and the following message is drawn.

Oracle 9i provides a lot of exception handling, and readers can try to modify a program that works correctly and execute a modified program to discover which exception handlers are invoked, and here's how to customize exception handling.

Custom Exception Handling

1. Define Exception handling
The syntax for defining exception handling is as follows:
Declare
Exception name exception;
2. Trigger Exception Handling
The syntax for triggering exception handling is as follows:
Raise exception name;
3. Handling Exceptions
After the exception handling is triggered, you can define the exception handling section, which is the following syntax:
Exception
When exception name 1 then
Exception handling statement segment 1;
When exception name 2 Then
Exception handling statement segment 2;
4. Examples
The following Pl/sql program contains the complete process of defining, triggering, and handling exception handling. Define an exception named Salaryerror, find the empno=7566 record in the Scott.emp datasheet, place its value in the variable tempsal, and determine if the Tempsal value is not between 900 and 2600, indicating that the employee's salary is problematic and will activate exception handling. Hint information.
Execute the following pl/sql code in "Sqlplus Worksheet", as shown in Figure 9.57.

"Matching program Location": 9th Chapter \ Exceptiondefine.sql.



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.