今天排查问题,检查出一个比较触发器中使用Exception的错误之处:
如下是用来检查两条工单的客户id是否一致的程序代码,如果不一致就报出提示:错误:新产生工单的用户和原来工单不一致!
begin
select customer_id
into ls_app_cus_id
from wo_service_info f
where f.row_id = :new.appeal_wo_id;
if ls_app_cus_id <> :new.customer_id then
raise_application_error('-20001',
'错误:新产生工单的用户和原来工单不一致!');
null;
end if;
exception
when others then
null;
end;
经排查,发现这种使用方式不正确,因为 raise_application_error 会被外面的Exception捕获到,捕获以后执行了 null;
应该使用如下方式进行捕获自定义异常:
begin
select customer_id
into ls_app_cus_id
from wo_service_info f
where f.row_id = :new.appeal_wo_id;
if ls_app_cus_id <> :new.customer_id then
raise myraise;
end if;
exception
when myraise then
raise_application_error('-20001',
'错误:新产生工单的用户和原来工单不一致!');
when others then
null;
end;
定义一个自定义类型的异常:myraise,然后在exception中使用when语句进行判断