设为首页 加入收藏

TOP

异常处理
2015-07-24 11:58:29 来源: 作者: 【 】 浏览:6
Tags:异常 处理

异常处理

一、预定义异常:

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;

】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
分享到: 
上一篇数据库存储过程简介与实例 下一篇sys_refcursor的用法实例

评论

帐  号: 密码: (新用户注册)
验 证 码:
表  情:
内  容:

·Error generating ar (2025-12-25 11:24:46)
·Http 和 Https的区别 (2025-12-25 11:24:44)
·python数据分析岗的 (2025-12-25 10:02:21)
·python做数据分析需 (2025-12-25 10:02:19)
·成为一个优秀的pytho (2025-12-25 10:02:16)