PL/SQL learning notes-Exception Handling

Source: Internet
Author: User

I. predefined exception errors
First lookCode:

Declare mytitle labor. xland. title % type; beginselect title into mytitle from labor. xland where State = 2; dbms_output.put_line (mytitle); exceptionwhen no_data_found thendbms_output.put_line ('No data found'); end;

This sectionProgramOutput data when data is retrieved
If no data is found, no data is found in the output.
No_data_found is a predefined error type.
For more predefined exception errors, see:
Http://www.cnblogs.com/liulun/articles/1526177.html

Ii. Non-predefined exception errors
First look at the Code:

Declare v_sqlcode number; v_sqlerrm varchar2 (2048); begininsert into labor. xland values (null, '20140901', 1); exceptionwhen no_data_found then dbms_output.put_line ('no data found '); when others then if sqlcode =-1111 then v_sqlcode: = sqlerrm; dbms_output.put_line (to_char (v_sqlcode); dbms_output.put_line (v_sqlerrm); end if; end;

When the block does not end with end
If block must end with end if
Sqlcode is an error code
Sqlerrm indicates the error message returned by Oracle.
This program outputs:

-1400ora-01400: NULL cannot be inserted ("labor". "xland". "title ")

In addition, The Pragma exception_init (name, errcode) function is used to handle non-predefined exceptions.
For example:

Declare v_exception exception; Pragma exception_init (v_exception,-1400); begininsert into labor. xland values (null, '20140901', 1); exceptionwhen no_data_found then dbms_output.put_line ('no data found '); When v_exception then dbms_output.put_line ('no data found 1'); End;

A new data type exception is mentioned here.
Pragma exception_init (name, errcode)
Assign the error code-1400 to v_exception.
Sqlcode and other system variables cannot be used in the second when clause.

3. custom exception and throw
First look at the code

Declare v_exception exception; beginraise v_exception; exceptionwhen v_exception then dbms_output.put_line ('catch exception'); end;

First define an exception
Then throw this exception
Then capture this exception
That's all.

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.