下面举个例子来演示oracle的异常捕获过程:
/*
模拟存储过程的异常捕获
*/
create or replace procedure TestExceptionTest(inputvalue VARCHAR2) AS
v_div number; --除数
Err_one EXCEPTION; --抛出异常1
Err_two EXCEPTION; --抛出异常2
Err_trhee EXCEPTION; --抛出异常3
BEGIN
IF inputvalue = 1 THEN
RAISE Err_one;
END IF;
IF inputvalue = 2 THEN
RAISE Err_two;
END IF;
IF inputvalue = 3 THEN
RAISE Err_trhee;
END IF;
v_div := inputvalue / 0;
DBMS_OUTPUT.put_line(v_div);
EXCEPTION
WHEN Err_one THEN
DBMS_OUTPUT.PUT_LINE('异常1');
WHEN Err_two THEN
DBMS_OUTPUT.PUT_LINE('异常2');
WHEN Err_trhee THEN
DBMS_OUTPUT.PUT_LINE('异常3');
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE(TO_CHAR(SQLCODE) || '---' ||
SUBSTRB(SQLERRM, 1, 200));
END TestExceptionTest;
测试一下:
SQL> set serverout on;
SQL> exec TestExceptionTest(1);
异常1
PL/SQL procedure successfully completed
SQL> exec TestExceptionTest(2);
异常2
PL/SQL procedure successfully completed
SQL> exec TestExceptionTest(3);
异常3
PL/SQL procedure successfully completed
SQL> exec TestExceptionTest(4);
-1476---ORA-01476: 除数为 0
PL/SQL procedure successfully completed
SQL>