异常是程序执行是出现的事件,它会打断程序的正常执行.
异常分以下几类:
1.预定义异常,它是由plsql预先定义,如:
   NO_DATA_FOUND,
   TOO_MANY_ROWS
   VALUE_ERROR
   ...................
2.非预定义异常,它是处理与预定义异常无关的错误.
   要使用非预定义异常,要经过以下三步:
   1).定义异常标识符
   2).错误号与异常之间建立关联
   3).捕捉异常
   例子:    
 declare
declare
 e_my_exception Exception;
    e_my_exception Exception;
 pragma Exception_init(my_exception,-2220);
    pragma Exception_init(my_exception,-2220);
 begin
   begin
 
    


 ..
..
 Exception
   Exception
 when my_exception then
      when my_exception then
 
          


 End;
   End;3.自定义异常,自定义异常是用户自己定义的异常.自定义异常一般不是处理执行发生的错误,而是处理业务规则错误.
   要使用自定义异常,需要经过以下几个步骤:
   1.定义异常
   2.触发异常
   3.捕获异常
   例子:       
 declare
declare
 e_my_exception Exception;
    e_my_exception Exception;
 pragma Exception_init(my_exception,-2220);
    pragma Exception_init(my_exception,-2220);
 begin
   begin
 update tableName set=name='hui' where id='1234'
    update tableName set=name='hui' where id='1234'
 if sql%notfound then
    if sql%notfound then
 raise e_my_exception ;
        raise e_my_exception ;
 end if;
   end if;
 Exception
  Exception
 when my_exception then
      when my_exception then
 
          


 End;
   End;
 
   4.异常处理函数
    1.raise_application_error, 该过程只适用于数据库子过程(过程,函数,包触发器).
    2.sqlcode 捕获错误码
    3.sqlerrm 捕获错误消息
    例子: 
 Begin
Begin
 update
     update


 if sql%notfound then
     if sql%notfound then
 raise_application_error(-20008,'没有数据');
        raise_application_error(-20008,'没有数据');
 end if
     end if
 Exception
Exception 
 when others then
        when others then
 dbms_out.put_line('错误号:'||sqlcode);
            dbms_out.put_line('错误号:'||sqlcode);
 dbms_out.put_line('错误号:'||sqlerrm);
            dbms_out.put_line('错误号:'||sqlerrm);
 end;
end;5.编译警告
   alter session set plsql_warnings='enable:all';