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.