SQL> select * from book;
BNUM BNAME CREATETIME
---------- ---------------- --------------
1 c# 09-12月-09
2 vb 09-12月-09
--存储过程如下
create or replace procedure exception_test (b_num in number)
declare
my_exception exception;
booknum number;
myvar varchar2(16);
begin
begin
select count(bnum) into booknum from book
where bnum = b_num;
if booknum >=1 then
raise my_exception;
else
dbms_output.put_line('数据不存在!');
end if;
end;
begin
select bname into myvar from book
where bnum=b_num;
dbms_output.put_line('1号书名为'|| myvar);
exception
when no_data_found
then dbms_output.put_line('B块异常!');
end;
end;
-----------测试结果 -------------------------------------
SQL> execute exception_test (1);
A块异常
clz
PL/SQL 过程已成功完成。
SQL> execute exception_test (3);
数据不存在!
B块异常!
1号书名为c#
PL/SQL 过程已成功完成。
结束语 SQL中异常和其它程序一样,如果当前块中异常已被处理,它会继续向下走,直到块结束或是再出异常。如果当前块中异常没有被处理,它会向外层抛出。