北大青鸟oracle学习笔记18

来源:互联网 发布:程序员起个网名 编辑:程序博客网 时间:2024/05/29 08:30

异常

预定义异常
oracle为常见错误预定义
在DBMS_STANDARD程序包中提供了这些定义
不需要显示声明
declare
  sex student.stu_sex%type;
begin
  select stu_sex into sex from student;
  dbms_output.put_line('sex:'||sex);
exception
  when no_data_found then
    dbms_output.put_line('no such student!');
  when too_many_rows then
    dbms_output.put_line('too many rows selected!');
  when others then
    dbms_output.put_line('other error!');
end;

预定义异常名产生原因ACCESS_INTO_NULL未定义对象CASE_NOT_FOUNDCASE中若为包含相应的WHEN,并且没有设置ELSE时COLLECTION_IS_NULL集合元素未初始化CURSER_ALREADY_OPEN游标已经打开DUP_VAL_ON_INDEX唯一索引对应的列上有重复值INVALID_CURSOR在不合法的游标上进行操作INVALID_NUMBER内嵌的SQL语句不能将字符转换为数字NO_DATA_FOUND使用select into 未返回行,或应用索引表未初始化元素时TOO_MANY_ROWS执行select into时机过集超过一行ZERO_DIVIDE除数为0SUBSCRIPT_BEYOND_COUNT元素下标超过嵌套表或VARRAY的最大值SUBSCRIPT_OUTSIDE_LIMIT使用嵌套表或VARRAY时将下标制定为负数VALUE_ERROR赋值时,变量长度不足以容纳实际数据LOGIN_DENIEDPL/SQL 应用程序连接到 oracle 数据库时,提供了不正确的用户名或密码NOT_LOGGED_ONPL/SQL 应用程序在没有连接 oralce 数据库的情况下访问数据PROGRAM_ERRORPL/SQL 内部问题,可能需要重装数据字典& pl./SQL 系统包ROWTYPE_MISMATCH宿主游标变量与 PL/SQL 游标变量的返回类型不兼容SELF_IS_NULL使用对象类型时,在 null 对象上调用对象方法STORAGE_ERROR运行 PL/SQL 时,超出内存空间SYS_INVALID_ID无效的 ROWID 字符串TIMEOUT_ON_RESOURCEOracle 在等待资源时超时

 

用户自定义异常
声明类型为Exception类型
只能主动由raise抛出
declare
  Dup_Value Exception;
  icount int := 0;
begin
  select count(*) into icount from student;
  if icount > 0 then
    raise Dup_Value;
  end if;
exception
  when Dup_Value then
    dbms_output.put_line('the student is already existed!') ;
  when others then
    dbms_output.put_line('other error!');
end;

但是这里异常只能由raise抛出,若要在插入重复字段是返回自定义提示信息,需要使用编译指示

declare
  Dup_Value Exception;
  pragma Exception_init(Dup_Value,-1);
begin
  insert into student values('1','1','1',sysdate);
exception
  when Dup_Value then
    dbms_output.put_line('the student is already existed!') ;
  when others then
    dbms_output.put_line('other error!');
end;

pragma Exception_init(Dup_Value,-1); 这里后面一个参数是oracle错误代码ORA-00001

查询预定义异常,可以得知自定义异常方法
select text from dba_source where name='STANDARD' and text like '%EXCEPTION_INIT%';
TEXT
----------------------------------------------------------------
    pragma EXCEPTION_INIT(CURSOR_ALREADY_OPEN, '-6511');
    pragma EXCEPTION_INIT(DUP_VAL_ON_INDEX, '-0001');
    pragma EXCEPTION_INIT(TIMEOUT_ON_RESOURCE, '-0051');
    pragma EXCEPTION_INIT(INVALID_CURSOR, '-1001');
    pragma EXCEPTION_INIT(NOT_LOGGED_ON, '-1012');
    pragma EXCEPTION_INIT(LOGIN_DENIED, '-1017');
    pragma EXCEPTION_INIT(NO_DATA_FOUND, 100);
    pragma EXCEPTION_INIT(ZERO_DIVIDE, '-1476');
    pragma EXCEPTION_INIT(INVALID_NUMBER, '-1722');
    pragma EXCEPTION_INIT(TOO_MANY_ROWS, '-1422');
    pragma EXCEPTION_INIT(STORAGE_ERROR, '-6500');
    pragma EXCEPTION_INIT(PROGRAM_ERROR, '-6501');
    pragma EXCEPTION_INIT(VALUE_ERROR, '-6502');
    pragma EXCEPTION_INIT(ACCESS_INTO_NULL, '-6530');
    pragma EXCEPTION_INIT(COLLECTION_IS_NULL , '-6531');
    pragma EXCEPTION_INIT(SUBSCRIPT_OUTSIDE_LIMIT,'-6532');
    pragma EXCEPTION_INIT(SUBSCRIPT_BEYOND_COUNT ,'-6533');
    pragma EXCEPTION_INIT(ROWTYPE_MISMATCH, '-6504');
    pragma EXCEPTION_INIT(SYS_INVALID_ROWID, '-1410');
    pragma EXCEPTION_INIT(SELF_IS_NULL, '-30625');
    pragma EXCEPTION_INIT(CASE_NOT_FOUND, '-6592');
    pragma EXCEPTION_INIT(USERENV_COMMITSCN_ERROR, '-1725');
    pragma EXCEPTION_INIT(NO_DATA_NEEDED, '-6548');
    pragma EXCEPTION_INIT(INVALID_USERENV_PARAMETER, -2003);
    pragma EXCEPTION_INIT(ICD_UNABLE_TO_COMPUTE, -6594);
25 rows selected.

Raise_Application_Error(错误编号,'提示信息');
用于创建用户自定义的错误消息的过程
将应用程序专有的错误从服务器端转达到客户端应用程序
既可以在可执行部分中使用,也可以在异常部分中使用
错误编号必须介于-20000~-20999之间
错误消息的长度可长达2048个字节

declare
  Dup_Value Exception;
  pragma Exception_init(Dup_Value,-1);
begin
  insert into student values('1','1','1',sysdate);
exception
  when Dup_Value then
    raise_application_error(-20001,'the student is already existed!');
  when others then
    dbms_output.put_line('other error!');
end;

Error report:
ORA-20001: the student is already existed!
ORA-06512: 在 line 8

原创粉丝点击