Presentation is loading. Please wait.

Presentation is loading. Please wait.

Database Programming Using Oracle 11g

Similar presentations


Presentation on theme: "Database Programming Using Oracle 11g"— Presentation transcript:

1 Database Programming Using Oracle 11g
Error Handling and Exceptions

2 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

3 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

4 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

5 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

6 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

7 Error Handling and Exceptions
Type of Exceptions Internal Internal exceptions are define Raised automatically

8 Error Handling and Exceptions
Type of Exceptions User Define User should define it User should raise the exception

9 Error Handling and Exceptions
Formation of Built – in Exception Three component Exception Name Exception unique number Description

10 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.

11 Error Handling and Exceptions
Formation of Exception Build – in Exceptions unique numbers are system generated

12 Error Handling and Exceptions
Syntax of Exception

13 Error Handling and Exceptions
Declare Section All the declarations Begin Executable statements Exception Section – One per block Exception handling statements End;

14 Error Handling and Exceptions
Syntax of Exception One Exception section per block Nested blocks can have separate exception section

15 Error Handling and Exceptions
Scope of Exception

16 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;

17 Error Handling and Exceptions
Implementing Build-In Exception -I

18 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);

19 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;

20 Error Handling and Exceptions
Implementing Build-In Exception -II

21 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');

22 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;

23 Error Handling and Exceptions
Implementing Built-In Exception and SQL-III

24 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');

25 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;

26 Error Handling and Exceptions
Implementing Nested Exceptions-I

27 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);

28 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;

29 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;

30 Error Handling and Exceptions
Implementing Nested Exceptions-II

31 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;

32 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');


Download ppt "Database Programming Using Oracle 11g"

Similar presentations


Ads by Google