异常EXCEPTION

参考博文: http://blog.csdn.net/vking_wang/article/details/9110899

预定义异常

常见ORACLE异常错误:

非预定义异常

oralce预定义的异常对应的是相应的错误,但是还会有其他错误,但是这些错误并没有预先定义异常,因此,在出现非预定义异常的时候,我们可以将自定义的异常与oracle错误关联起来,

SQL> declare
  2      no_result EXCEPTION;
  3      v_empno emp.empno%TYPE:=&empno;
  4  begin
  5      update emp set sal=sal+100 where empno=v_empno;
  6      if SQL%notfound
  7      then
  8          raise no_result;
  9      end if;
10  EXCEPTION
11      when no_result then
12      dbms_output.put_line('无此数据,因此更新失败了');
13  end;
14  /
无此数据,因此更新失败了
PL/SQL procedure successfully completed

1

用户自定义异常

SQL> declare
  2      no_result EXCEPTION;
  3      v_empno emp.empno%TYPE:=&empno;
  4  begin
  5      update emp set sal=sal+100 where empno=v_empno;
  6      if SQL%notfound
  7      then
  8          raise no_result;
  9      end if;
10  EXCEPTION
11      when no_result then
12      dbms_output.put_line('无此数据,因此更新失败了');
13  end;
14  /
无此数据,因此更新失败了
PL/SQL procedure successfully completed

1

RAISE_APPLICATION_ERROR

该函数只能在子程序(过程,函数,包,触发器)中使用,不能再匿名块和客户端的子程序中使用
语法如下:
RAISE_APPLICATION_ERROR(error_number, error_message, [keep_errors] );
error_number 是从 –20,000 到 –20,999 之间的参数,这样就不会与 ORACLE 的任何错误代码发生冲突
error_message 是相应的提示信息(< 2048 字节),
keep_errors 为可选,如果keep_errors =TRUE ,则新错误将被添加到已经引发的错误列表中。如果keep_errors=FALSE(缺省),则新错误将替换当前的错误列表。

posted @ 2017-10-30 14:05  岳麓丹枫  阅读(212)  评论(0编辑  收藏  举报