Home  >  Article  >  Database  >  异常处理

异常处理

WBOY
WBOYOriginal
2016-06-07 16:01:051081browse

异常处理 一、预定义异常: Dup_val_on_index:试图向唯一索引列插入重复值,相当于建立了一个唯一索引 然后再向这个索引的列中插入一条重复的数据就会异常。 Invalid_cursors:试图进行非法的游标操作 Invalid_number : 试图将字符串转换为数字 no_data_found

异常处理

一、预定义异常:

Dup_val_on_index:试图向唯一索引列插入重复值,相当于建立了一个唯一索引 然后再向这个索引的列中插入一条重复的数据就会异常。

Invalid_cursors:试图进行非法的游标操作

Invalid_number : 试图将字符串转换为数字

no_data_found : select into 语句没有返回任何记录

Too_many_rows : select into 语句返回超过1条记录

Zero_divide : 试图除以 0

Cursor_already_open : 试图打开一个已经打开的游标

Others :包括所有异常

二、异常处理语法结构:

EXCEPTION --异常开始

WHEN 异常名1 THEN

--对应的异常处理

1、实例:

DECLARE

newSal emp.sal%TYPE;

BEGIN

select sal into newSal from emp;

EXCEPTION

when too_many_rows then --超过记录用too_many_rows

dbms_output.put_line('返回的记录太多了');

when others then --其他异常则输出

dbms_output.put_line('未知异常');

END;

三、自定义异常:

DECLARE

newSal emp.sal%TYPE;

myexp exception; --声明异常变量

BEGIN

select sal into newSal from emp where rownum=1;

if newsal>500 then --情况出现时就调用异常

raise myexp;

end if;

EXCEPTION

when too_many_rows then --超过记录用too_many_rows

dbms_output.put_line('返回的记录太多了');

when myexp then

dbms_output.put_line('工资不能超过500');

when others then --其他异常则输出

dbms_output.put_line('未知异常');

END;

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn