|
常用异常处理
declare
e_duplicate_name exception;
v_ename emp.ename%type;
e_newname emp.ename%type := 'smith';
begin
select ename into v_ename from emp where empno = 7639;
if v_ename = v_newname then
raise e_duplicate_name;
end if;
insert into emp values (23232, 3244, v_newname);
exception
when e_duplicate_name then
dbms_output.put_line('duplicate name');
when others then
dbms_output.put_line(sqlcode || sqlerrm);
end;
主要是为了对ora-类的不是预定义异常的异常添加个名称。
declare
e_missingnull exception;
pragma exception_init(e_missingnull, -1400);
begin
insert into empbak (empno) values (null);
commit;
exception
when e_missingnull then
dbms_output.put_line('触发了1400错误' || sqlerrm);
rollback;
end;
/
触发了1400错误ORA-01400: 无法将 NULL 插入 ("BAIXYU"."EMPBAK"."EMPNO")
raise_application_error在存储的子程序中调用。
|