您的位置:首页 > 其它

[bbk5355]第18集 - Chapter 08 - Handling Exceptions(01)

2013-04-24 12:10 567 查看

Exception Types

Predefined Oracle Server --Implicitly raised

Non-predefined Oracle Server --Implicitly raised

User-defined                        --Explicitly raised

Syntax to Trap Exceptions

cannot insert NULL

DECLARE
e_insert_excep EXCEPTION;
/*
PRAGMA EXCEPTION_INIT(e_insert_excep,-01400)功能:
将异常名称e_insert_excep与error code -01400进行关联起来,以后直接引用此名称就相当于引用此代码;
*/
PRAGMA EXCEPTION_INIT(e_insert_excep,-01400);
BEGIN
INSERT INTO dept(department_id,department_name) VALUES (280,NULL);
EXCEPTION
WHEN e_insert_excep THEN
DBMS_OUTPUT.PUT_LINE('INSERT OPERATION FAILD');
DBMS_OUTPUT.PUT_LINE(SQLERRM);
END;

/
SQL> @2
INSERT OPERATION FAILD
ORA-01400: cannot insert NULL into ("HR"."DEPT"."DEPARTMENT_NAME")

PL/SQL procedure successfully completed.


EXCEPTION_INIT Directive

EXCEPTION_INIT is a complie-time command or gragma used to associate a name with an internal error code.

EXCEPTION_INIT instructs the compiler to associate an identifier,declared as an EXCEPTION,with a specific error number.

Syntax:

DECLARE

  exception_name EXCEPTION;

  PRAGMA EXCEPTION_INIT(exception_name,integer);


Functions for Trapping Exceptions

SQLCODE:Returns the numeric value for the error code.

SQLERRM:Returns the message associated with the erro number.

孪生兄弟,并行出现;值被自动填充.

SQLCODE、SQLERRM是2个函数.

Functions for Trapping Exceptions

DECLARE
error_code      NUMBER;
error_message   VARCHAR2(255);
BEGIN
...
EXCEPTION
...
WHEN OTHERS THEN
ROLLBACK;
error_code := SQLCODE;
error_messsage :=SQLERRM;
INSERT INTO errors(e_user,e_date,error_code,error_message)
VALUES(USER,SYSDATE,error_code,error_message);
END;

/


通过上述demo延伸问题:

当BEGIN section里面的语句发生异常的时候,此时进入到EXCEPTION section,此时有ROLLBACK clause,但是紧接着又有INSERT 日志文件的语句发起了一个新的事务,那么此时就有必要进行指明ROLLBACK到哪一个事务,不ROLLBACK到即将用到的事务:
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: