Download presentation
Presentation is loading. Please wait.
Published byEthan Harris Modified over 9 years ago
1
Oracle 8i Exception Handling
2
General Syntax DECLARE --- BEGIN --- EXCEPTION WHEN exception_name1 THEN -Error handling statements WHEN exception_name2 THEN -Error handling statements WHEN OTHERS THEN -Error handling statements END; DECLARE --- BEGIN --- EXCEPTION WHEN exception_name1 THEN -Error handling statements WHEN exception_name2 THEN -Error handling statements WHEN OTHERS THEN -Error handling statements END;
3
Example BEGIN DELETE FROM product WHERE product_id = 52; EXCEPTION WHEN OTHERS THEN Dbms_output.Put_line ('An unknown error has occurred'); END; BEGIN DELETE FROM product WHERE product_id = 52; EXCEPTION WHEN OTHERS THEN Dbms_output.Put_line ('An unknown error has occurred'); END;
4
ERRORS COLLECTION_IS_NULL CURSOR_ALREADY_OPEN DUP_VAL_ON_INDEX INVALID_CURSOR LOGIN_DENIED NO_DATA_FOUND PROGRAM_ERROR STORAGE_ERROR SUBSCRIPT_OUTSIDE_LIMIT TOO_MANY_ROWS VALUE_ERROR ZERO_DIVIDE COLLECTION_IS_NULL CURSOR_ALREADY_OPEN DUP_VAL_ON_INDEX INVALID_CURSOR LOGIN_DENIED NO_DATA_FOUND PROGRAM_ERROR STORAGE_ERROR SUBSCRIPT_OUTSIDE_LIMIT TOO_MANY_ROWS VALUE_ERROR ZERO_DIVIDE
5
EXAMPLE BEGIN... EXCEPTION WHEN TOO_MANY_ROWS THEN Dbms_output.Put_line ( 'The SELECT Statement returned more than one row, where only one row was expected'); END; BEGIN... EXCEPTION WHEN TOO_MANY_ROWS THEN Dbms_output.Put_line ( 'The SELECT Statement returned more than one row, where only one row was expected'); END;
6
Unnamed System Exceptions Unnamed System Exceptions are predefined Oracle exceptions that have not been assigned a name because they are raised less frequently.
7
Unnamed System Exceptions DECLARE exception_name EXCEPTION; PRAGMA EXCEPTION_INIT (exception_name, Err_code); BEGIN DECLARE exception_name EXCEPTION; PRAGMA EXCEPTION_INIT (exception_name, Err_code); BEGIN
8
Example DECLARE child_record_present EXCEPTION; PRAGMA EXCEPTION_INIT (child_record_present, -2292); BEGIN DELETE FROM product WHERE product_id= 55; EXCEPTION WHEN child_record_present THEN Dbms_output.Put_line('There are transactions on this product. Please delete the transactions on this product first'); END; DECLARE child_record_present EXCEPTION; PRAGMA EXCEPTION_INIT (child_record_present, -2292); BEGIN DELETE FROM product WHERE product_id= 55; EXCEPTION WHEN child_record_present THEN Dbms_output.Put_line('There are transactions on this product. Please delete the transactions on this product first'); END;
9
User-Defined Exceptions User-Defined Exceptions result from violations of business rules that do not appear as errors to Oracle. They should be explicitly declared They should be explicitly raised in the Execution section They should be handled by referencing the user-defined exception name within the Exception section User-Defined Exceptions result from violations of business rules that do not appear as errors to Oracle. They should be explicitly declared They should be explicitly raised in the Execution section They should be handled by referencing the user-defined exception name within the Exception section
10
DECLARE Out_of_bounds EXCEPTION; CURSOR sal_cur IS SELECT salary FROM sales_person; v_salary sales_person.salary%type; new_salary CONSTANT sales_person.salary%type := 40000; v_message VARCHAR2(200); BEGIN FOR sal_rec IN sal_cur LOOP v_salary := sal_rec.salary; IF new_salary < v_salary THEN v_message := sal_cur%rowcount || 'New Salary is less than the current salary! The remaining rows will not be processed'; RAISE out_of_bounds; ELSIF new_salary = v_salary THEN v_message := 'No change in salary.'; ELSE v_message := 'New Salary is more than the current salary.'; END IF; Dbms_output.Put_line (sal_cur%rowcount || v_message); END LOOP; EXCEPTION WHEN out_of_bounds THEN Dbms_output.Put_line (v_message); END; DECLARE Out_of_bounds EXCEPTION; CURSOR sal_cur IS SELECT salary FROM sales_person; v_salary sales_person.salary%type; new_salary CONSTANT sales_person.salary%type := 40000; v_message VARCHAR2(200); BEGIN FOR sal_rec IN sal_cur LOOP v_salary := sal_rec.salary; IF new_salary < v_salary THEN v_message := sal_cur%rowcount || 'New Salary is less than the current salary! The remaining rows will not be processed'; RAISE out_of_bounds; ELSIF new_salary = v_salary THEN v_message := 'No change in salary.'; ELSE v_message := 'New Salary is more than the current salary.'; END IF; Dbms_output.Put_line (sal_cur%rowcount || v_message); END LOOP; EXCEPTION WHEN out_of_bounds THEN Dbms_output.Put_line (v_message); END;
11
RAISE_APPLICATION_ERROR ( ) A built-in procedure capable of displaying user-defined error messages at the SQL host environment along with the error number whose range is in between -20000 and -20999. In a PL/SQL block, this procedure is used to display messages whenever user- defined exceptions are raised. A built-in procedure capable of displaying user-defined error messages at the SQL host environment along with the error number whose range is in between -20000 and -20999. In a PL/SQL block, this procedure is used to display messages whenever user- defined exceptions are raised.
12
Example DECLARE e_num EXCEPTION; num1 NUMBER := &num1; BEGIN IF num1 > 100 THEN RAISE e_num; END IF; Dbms_output.Put_line ('The value is: ' || num1); EXCEPTION WHEN e_num THEN raise_application_error(-20900, 'value too High!'); END; DECLARE e_num EXCEPTION; num1 NUMBER := &num1; BEGIN IF num1 > 100 THEN RAISE e_num; END IF; Dbms_output.Put_line ('The value is: ' || num1); EXCEPTION WHEN e_num THEN raise_application_error(-20900, 'value too High!'); END;
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.