Database Programming Using Oracle 11g Error Handling and Exceptions
Error Handling and Exceptions Error Handling and Exception: Exception vs Error Compile time syntax issues are Error Run-time errors are usually Exceptions. Semantic error are exceptions
Error Handling and Exceptions Need for Exception Handling It is practically not possible to foresee all problematic events Exception cause the program to terminate abnormally
Error Handling and Exceptions Need for Exception Handling Flow of program is broken and control is lost from developer view point Even the best programmer can have bugs in code
Error Handling and Exceptions Handling Exceptions Special block in PL/SQL to handle exceptions. Run-time error can be handled to avoid abnormal termination of program
Error Handling and Exceptions How Exception Handling Work When run-time error is generated Exception is raised. Current flow stop. Control is transferred to Exception block of PL/SQL
Error Handling and Exceptions Type of Exceptions Internal Internal exceptions are define Raised automatically
Error Handling and Exceptions Type of Exceptions User Define User should define it User should raise the exception
Error Handling and Exceptions Formation of Built – in Exception Three component Exception Name Exception unique number Description
Error Handling and Exceptions DUP_VAL_ON_INDEX ORA-00001 Exception raised when you store duplicate value in unique constraint column. CASE_NOT_FOUND ORA-06592 Exception raised when no any choice case found in CASE statement as well as no ELSE clause in CASE statement.
Error Handling and Exceptions Formation of Exception Build – in Exceptions unique numbers are system generated
Error Handling and Exceptions Syntax of Exception
Error Handling and Exceptions Declare Section All the declarations Begin Executable statements Exception Section – One per block Exception handling statements End;
Error Handling and Exceptions Syntax of Exception One Exception section per block Nested blocks can have separate exception section
Error Handling and Exceptions Scope of Exception
Error Handling and Exceptions Declare Section All the declarations Begin Executable statements Exception handling statements declare – Inner block begin exception – local to innerblock End; Exception Section – Global exception end;
Error Handling and Exceptions Implementing Build-In Exception -I
Error Handling and Exceptions DECLARE stock_price NUMBER := 9.73; net_earnings NUMBER := 0; pe_ratio NUMBER; BEGIN pe_ratio := stock_price / net_earnings; DBMS_OUTPUT.PUT_LINE('Price/earnings ratio = ' || pe_ratio);
Error Handling and Exceptions WHEN ZERO_DIVIDE THEN DBMS_OUTPUT.PUT_LINE('Company must have had zero earnings.'); pe_ratio := NULL; WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('Some other kind of error occurred.'); END;
Error Handling and Exceptions Implementing Build-In Exception -II
Error Handling and Exceptions declare grade varchar2(1):='H'; begin CASE WHEN grade = 'A' THEN dbms_output.put_line('Excellent'); WHEN grade = 'B' THEN dbms_output.put_line('Very Good'); WHEN grade = ‘C' THEN dbms_output.put_line('Fair');
Error Handling and Exceptions WHEN grade = ‘D' THEN dbms_output.put_line('Poor'); END CASE; exception When case_not_found then dbms_output.put_line(‘No Case match'); When others then dbms_output.put_line ('In the exception block‘ || SQLERRM || SQLCode); end;
Error Handling and Exceptions Implementing Built-In Exception and SQL-III
Error Handling and Exceptions declare name varchar2(30):=''; begin select ename into name from emp; exception when no_data_found then dbms_output.put_line('Record not matched');
Error Handling and Exceptions when DUP_VAL_ON_INDEX then dbms_output.put_line('Unique Key Violated'); when others then dbms_output.put_line(SQLCODE); dbms_output.put_line(SQLERRM); end;
Error Handling and Exceptions Implementing Nested Exceptions-I
Error Handling and Exceptions declare name varchar2(30); begin select ename into name from emp where empno=7369; dbms_output.put_line(name); insert into emp (empno) values(7369);
Error Handling and Exceptions dbms_output.put_line(name); exception when dup_val_on_index then dbms_output.put_line('Duplicated values in inner block'); when others then dbms_output.put_line('In inner block'); dbms_output.put_line(SQLCODE); end;
Error Handling and Exceptions when NO_data_found then dbms_output.put_line('No Data Found in outer Block'); when others then dbms_output.put_line('No Data found in outer block'); end;
Error Handling and Exceptions Implementing Nested Exceptions-II
Error Handling and Exceptions declare name varchar2(30); begin select ename into name from emp where empno=7369; dbms_output.put_line(name); select ename into name from emp where empno=7338;
Error Handling and Exceptions dbms_output.put_line(name); exception when INVALID_NUMBER then dbms_output.put_line(SQLCODE); end; when NO_DATA_FOUND then dbms_output.put_line('No Data found in outer block');