exception
when first_exception then <handle first exception>
when second_exception then <handle second exception>
when others then <handle other exception>
pragma exception_init的用法
declare
e_emp_remaining EXCEPTION;
pragma exception_init(e_emp_remaining ,-2292);
begin
delete from dept where deptno=10;
commit;
EXCEPTION
when (e_emp_remaining then
dbms_output.put_line('cannot remove dept'||to_char(10)||'.employee exist.');
end;
declare
v_eno emp.empno%type :=&.empno;
not_found exception;
begin
update emp set sal=sal*1.1 where empno=v_eno;
if SQL% notfound then
raise not_found// 使用RAISE语句抛出异常
end if;
exception
when not_found then
dbms_output.put_line('You can't update the sal,the number does not!exist!');
when others then
dbms_output.put_line('other other');
end
CREATE OR REPALCE TRIGGER minimun_age_check
BEFORE INSERT ON employee
FOR EACH ROW
BEGIN
IF ADD_MONTHS( :new.birth_date, 18*12) > SYSDATE
THEN
RAISE_APPLICATION_ERROR(-20001, 'Employees must at least eighteen years of age.');
END IF;
END;
在客户端,你可以写一个类似下面的程序,来测试一下。
Sql代码
DECLARE
no_babies_allowedEXCEPTION;
/*将名称与用于触发器中的错误号码关联起来*/
PRAGMAEXCEPTION_INIT(no_babies_allowed,-20001);
BEGIN
INSERTINTOemployee....;
EXCEPTION
WHENno_babies_allowed
THEN
/*
||SQLERRM将传递给内置过程RAISE_APPLICATION_ERROR的消息返回
*/
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;
DECLARE
no_babies_allowed EXCEPTION;
/*将名称与用于触发器中的错误号码关联起来*/
PRAGMA EXCEPTION_INIT(no_babies_allowed, -20001);
BEGIN
INSERT INTO employee ....;
EXCEPTION
WHEN no_babies_allowed
THEN
/*
|| SQLERRM 将传递给内置过程 RAISE_APPLICATION_ERROR 的消息返回
*/
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;