极度郁闷,有一段时间没登陆51了,今天回来竟然把提示问题给弄丢了,猜了半个小时才猜出来。。。。

数据库之异常

上一篇 / 下一篇  2010-01-07 08:27:23 / 个人分类:Oracle

在运行程序时出现的错误叫异常.发生异常后,语句将停止执行.PL/SQL 引擎立即将控制权转到 PL/SQL 块的异常处理部分.
异常处理机制简化了代码中的错误检测.预定义的异常是在运行时由系统自动引发的,而用户自定义的异常必须使用 raise 语句显示引发.

异常格式:

begin
sequence_of_statements;
exception
when <exception_name> then
sequence_of_statements;
when others then --others 确保不会漏过任何异常
sequence_of_statements;
end;

用户定义异常格式:declare my_exception exception;

显示引发异常格式:raise my_exception;

引发应用程序错误格式:raise_application_error(error_number, error_message);
error_number --异常编号必须介于 -20000 和 -20999 之间的负整数
error_message --异常消息文本

--预定义异常

declare
ename_temp varchar2(20);
begin
select ename into ename_temp from emp;
exception
when too_many_rows then
dbms_output.put_line('返回多行');
end;

--用户定义异常

declare
test_exception exception;
temp varchar2(4);
begin
temp := '&temp';
if temp not in ('附件', '顶盖', '备件') then
raise test_exception;
else
dbms_output.put_line('您输入的类别是:' || temp);
end if;
exception
when test_exception then
dbms_output.put_line('无法识别该类别');
end;

--引发应用程序错误

declare
comm_temp emp.comm%type;
comm_exception exception;
begin
select nvl(comm, 0) into comm_temp from emp where empno = &empno;
if comm_temp = 0 then
raise comm_exception;
else
dbms_output.put_line('奖金是:' || comm_temp);
end if;
exception
when comm_exception then
raise_application_error(-20001, '没有附加工资');
end;

TAG: 数据库 异常

 

评分:0

我来说两句

Open Toolbar