PL/SQL exception error handling

PL/SQL exception error handling

An exception handling concept

Exception handling (It is used to deal with unexpected events in the normal execution process. Exception handling of the program block. Predefined errors and custom errors. Once the PL/SQL program block generates an exception and does not indicate how to handle it, the program will automatically terminate the entire The program runs

The exception handling part is generally placed in the second half of the PL/SQL program body. The structure is

EXCEPTION
	WHEN first_exception THEN <code to handle first exception>
	WHEN second_exception THEN <code to handle second exception>
	WHEN OTHERS THEN <code to handle others exception>
END
注意:异常处理可以按任意次序排列,但 OTHERS 必须放在最后

1 Predefined error

There are about 24 exceptions predefined by ORACLE. The treatment of this abnormal situation does not need to be defined in the program , it is automatically triggered by ORACLE .

(1) Pre-defined exception handling In
the part of the pre-defined description, the ORACLE exception error
Insert picture description here
Insert picture description here
handling of this exception situation only needs to directly quote the corresponding exception situation name in the exception handling part of the PL/SQL block, and complete the corresponding The exception error handling can be done.
(2) Case
Update the salary of the designated employee, if the salary is less than 3000, add 3000 to deal with the abnormal NO_DATA_FOUND, TOO_MANY _ROWS
Insert picture description here
2 non-predefined errors

That is, other standard ORACLE errors. The handling of this abnormal situation requires the user to define it in the program , and then it will be automatically triggered by ORACLE .

(1) Non-predefined exception handling
To define a non-predefined Oracle error, the steps are as follows:
①. Define the exception condition in the definition part of the PL/SQL block
<exception condition> EXCEPTION;
② Define the exception condition, In conjunction with standard ORACLE errors, use the PRAGMA EXCEPTION_INIT statement:
PRAGMA EXCEPTION_INIT(<abnormality>, error code>)>);
③ In the abnormality handling part of the PL/SQL block, handle the abnormality accordingly.
(2) Case
Insert picture description here
3 User-defined error

During the execution of the program, an abnormal situation that the programmer thinks occurs. Treatment of such abnormal condition, need the user defined in the program , and then explicitly in the program which triggers.

When an error related to an abnormal error occurs, the abnormal error is implicitly triggered. User-defined exception errors are triggered by explicitly using the RAISE statement. When an abnormal error is caused, the control is transferred to the abnormal error part of the EXCEPTION block, and the error
handling code is executed .
For the treatment of this kind of abnormal situation, the steps are as follows:
① Define the abnormal situation in the definition part of the PL/SQL block
<Exceptional situation> EXCEPTION;
② RAISE <Exceptional situation>;
③ Treat the abnormal situation in the abnormal situation handling part of the PL/SQL block Deal with it accordingly.
(2) Case
Update the salary of the designated employee and increase by 100. If the employee does not exist, a user-defined exception will be thrown: no_result
Insert picture description here

Two use SQLCODE, SQLERRM

SQLCODE: return the number of error code
SQLERRM: return the error message
such as
SQLCODE = -100 =》 SQLERRM='no_data_found '
SQLCODE = 0 =》 SQLERRM='normal, successfual completion'
Case:
(1) Store the ORACLE error code and its information in Error code table

CREATE TABLE errors (errnum NUMBER(4), errmsg VARCHAR2(100));
DECLARE
	err_msg VARCHAR2(100);
BEGIN
	/* 得到所有 ORACLE 错误信息*/
	FOR err_num IN -100 .. 0 LOOP
		err_msg := SQLERRM(err_num)
		INSERT INTO errors VALUES(err_num, err_msg);
	END LOOP
END;
DROP TABLE errors;

(2) Query ORACLE error code

BEGIN
	INSERT INTO emp(empno, ename, hiredate, deptno)
		VALUES(2222, ‘Jerry’, SYSDATE, 20);
	DBMS_OUTPUT.PUT_LINE(' 插入数据记录成功
	INSERT INTO emp(empno, ename, hiredate, deptno)
		VALUES(2222, ‘Jerr y’, SYSDATE, 20);
	DBMS_OUTPUT.PUT_LINE(' 插入数据记录成功
EXCEPTION
	WHEN OTHERS THEN
	DBMS_OUTPUT.PUT_LINE(SQLCODE||---‘SQLCODE||---||SQLERRM);
END;

Guess you like

Origin blog.csdn.net/hcyxsh/article/details/114984452