수색…
소개
오라클은 다양한 예외를 만들어냅니다. 불분명 한 메시지로 코드를 중지하는 것이 얼마나 지루한 지 놀라실 수 있습니다. PL / SQL 코드의 기능을 쉽게 개선하려면 가장 낮은 레벨에서 예외를 처리해야합니다. 카펫 아래에 예외를 숨기지 마십시오. 코드를 유지하고 다른 사람이 유지할 수 없으면 예외가 아닙니다.
연결 오류 예외 처리
각 표준 Oracle 오류는 오류 번호와 연관됩니다. 귀하의 코드에서 무엇이 잘못 될 수 있는지 예측하는 것이 중요합니다. 다음은 다른 데이터베이스에 연결하는 경우입니다.
-
-28000
계정이 잠김 -
-28001
암호가 만료되었습니다. -
-28002
유예 기간 -
-1017
잘못된 사용자 / 암호
다음은 데이터베이스 링크에서 사용되는 사용자의 문제를 테스트하는 방법입니다.
declare
v_dummy number;
begin
-- testing db link
execute immediate 'select COUNT(1) from dba_users@pass.world' into v_dummy ;
-- if we get here, exception wasn't raised: display COUNT's result
dbms_output.put_line(v_dummy||' users on PASS db');
EXCEPTION
-- exception can be referred by their name in the predefined Oracle's list
When LOGIN_DENIED
then
dbms_output.put_line('ORA-1017 / USERNAME OR PASSWORD INVALID, TRY AGAIN');
When Others
then
-- or referred by their number: stored automatically in reserved variable SQLCODE
If SQLCODE = '-2019'
Then
dbms_output.put_line('ORA-2019 / Invalid db_link name');
Elsif SQLCODE = '-1035'
Then
dbms_output.put_line('ORA-1035 / DATABASE IS ON RESTRICTED SESSION, CONTACT YOUR DBA');
Elsif SQLCODE = '-28000'
Then
dbms_output.put_line('ORA-28000 / ACCOUNT IS LOCKED. CONTACT YOUR DBA');
Elsif SQLCODE = '-28001'
Then
dbms_output.put_line('ORA-28001 / PASSWORD EXPIRED. CONTACT YOUR DBA FOR CHANGE');
Elsif SQLCODE = '-28002'
Then
dbms_output.put_line('ORA-28002 / PASSWORD IS EXPIRED, CHANGED IT');
Else
-- and if it's not one of the exception you expected
dbms_output.put_line('Exception not specifically handled');
dbms_output.put_line('Oracle Said'||SQLCODE||':'||SQLERRM);
End if;
END;
/
커스텀 예외를 정의하고, 그것을 올리고 그것이 어디에서 왔는지를 봅니다.
이것을 설명하기 위해 여기에 3 가지 "잘못된"동작을 갖는 함수가 있습니다.
- 매개 변수는 완전히 어리 석다 : 우리는 사용자 정의 표현식을 사용한다.
- 매개 변수에는 오타가 있습니다. Oracle 표준
NO_DATA_FOUND
오류를 사용합니다. - 처리되지 않은 다른 케이스
자유롭게 표준에 맞게 조정하십시오.
DECLARE
this_is_not_acceptable EXCEPTION;
PRAGMA EXCEPTION_INIT(this_is_not_acceptable, -20077);
g_err varchar2 (200) := 'to-be-defined';
w_schema all_tables.OWNER%Type;
PROCEDURE get_schema( p_table in Varchar2, p_schema out Varchar2)
Is
w_err varchar2 (200) := 'to-be-defined';
BEGIN
w_err := 'get_schema-step-1:';
If (p_table = 'Delivery-Manager-Is-Silly') Then
raise this_is_not_acceptable;
end if;
w_err := 'get_schema-step-2:';
Select owner Into p_schema
From all_tables
where table_name like(p_table||'%');
EXCEPTION
WHEN NO_DATA_FOUND THEN
-- handle Oracle-defined exception
dbms_output.put_line('[WARN]'||w_err||'This can happen. Check the table name you entered.');
WHEN this_is_not_acceptable THEN
-- handle your custom error
dbms_output.put_line('[WARN]'||w_err||'Please don''t make fun of the delivery manager.');
When others then
dbms_output.put_line('[ERR]'||w_err||'unhandled exception:'||sqlerrm);
raise;
END Get_schema;
BEGIN
g_err := 'Global; first call:';
get_schema('Delivery-Manager-Is-Silly', w_schema);
g_err := 'Global; second call:';
get_schema('AAA', w_schema);
g_err := 'Global; third call:';
get_schema('', w_schema);
g_err := 'Global; 4th call:';
get_schema('Can''t reach this point due to previous error.', w_schema);
EXCEPTION
When others then
dbms_output.put_line('[ERR]'||g_err||'unhandled exception:'||sqlerrm);
-- you may raise this again to the caller if error log isn't enough.
-- raise;
END;
/
정규 데이터베이스 제공 :
[WARN]get_schema-step-1:Please don't make fun of the delivery manager.
[WARN]get_schema-step-2:This can happen. Check the table name you entered.
[ERR]get_schema-step-2:unhandled exception:ORA-01422: exact fetch returns more than requested number of rows
[ERR]Global; third call:unhandled exception:ORA-01422: exact fetch returns more than requested number of rows
드문 경우를 처리하기 위해 예외가 있음을 기억하십시오. 모든 액세스에서 예외를 발생시킨 응용 프로그램을 보았습니다. 사용자 암호를 요청하고 "연결되지 않았습니다"라고 말하면서 계산 낭비가 너무 많았습니다.
Modified text is an extract of the original Stack Overflow Documentation
아래 라이선스 CC BY-SA 3.0
와 제휴하지 않음 Stack Overflow