例外の概念とシステム例外
2580 ワード
例外の例外は、プログラム設計言語が提供する機能であり、プログラムの堅牢性とフォールトトレランス性を強化するために使用されます.
ORACLEの例外システム例外 カスタム例外 システム例外 No_data_found(データが見つからない) Too_many_rows(select..into文は複数の行に一致する) Zero_Divide(ゼロで除かれた) Value_Error(算術または変換エラー) Timeout_on_リソース待機中にタイムアウトが発生したresource No_data_foundケース
Too_many_rowsケース
Zero_Divideケース
value_errorケース
カスタム例外変数を定義します.タイプはexception です. raiseを使用してカスタム例外 を投げ出す
ORACLEの例外
-- :no_data_found
SET SERVEROUTPUT ON
DECLARE
pe_first_name EMPLOYEES.FIRST_NAME%TYPE;
pe_last_name EMPLOYEES.LAST_NAME%TYPE;
BEGIN
-- 1234
select first_name,last_name into pe_first_name,pe_last_name from EMPLOYEES where EMPLOYEE_ID=1234;
exception
when no_data_found then DBMS_OUTPUT.PUT_LINE(' ');
when others then DBMS_OUTPUT.PUT_LINE(' ');
end;
/
Too_many_rowsケース
-- :too_many_rows
SET SERVEROUTPUT ON
DECLARE
--
pe_first_name EMPLOYEES.FIRST_NAME%TYPE;
pe_last_name EMPLOYEES.LAST_NAME%TYPE;
BEGIN
select first_name,last_name into pe_first_name,pe_last_name from EMPLOYEES where DEPARTMENT_ID=60;
exception
when too_many_rows then DBMS_OUTPUT.PUT_LINE('selectinto ');
when others then DBMS_OUTPUT.PUT_LINE(' ');
end;
/
Zero_Divideケース
-- : 0 zero_divide
SET SERVEROUTPUT ON
DECLARE
--
pnum number;
BEGIN
pnum := 1/0;
exception
when zero_divide then DBMS_OUTPUT.PUT_LINE('1:0 ');
DBMS_OUTPUT.PUT_LINE('2:0 ');
when others then DBMS_OUTPUT.PUT_LINE(' ');
end;
/
value_errorケース
-- :value_error
SET SERVEROUTPUT ON
DECLARE
--
pnum number;
BEGIN
pnum := 'abc';
exception
when value_error then DBMS_OUTPUT.PUT_LINE(' ');
when others then DBMS_OUTPUT.PUT_LINE(' ');
end;
/
カスタム例外
-- : 50
SET SERVEROUTPUT ON
DECLARE
-- , 50
cursor cemp is select first_name,last_name from EMPLOYEES where DEPARTMENT_ID = 300;
pe_first_name EMPLOYEES.first_name%type;
pe_last_name EMPLOYEES.LAST_NAME%TYPE;
--
no_emp_found EXCEPTION;
BEGIN
--
open cemp;
--
FETCH cemp into pe_first_name,pe_last_name;
if cemp%notfound then
--
raise no_emp_found;
end if;
--
--oracle pmon(process monitor)
close cemp;
exception
when no_emp_found then DBMS_OUTPUT.PUT_LINE(' ');
when others then DBMS_OUTPUT.PUT_LINE(' ');
end;
/