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
Advanced Exceptions

2 Advanced Exceptions Advanced Exceptions: Raise_Application_Error
Built in procedure It let user attached a ora-number and customized message to user Terminate any pl/sql block and behave like pre-define exception

3 Advanced Exceptions Raise vs Raise_Application_Error
Raise is used to call pre-defined or user-defined exception Raise_application_erorr let the developer show the customized message with number Call from front end

4 Database Programming Using Oracle 11g
Syntax of Raise_Application_Error

5 Error Handling and Exceptions
Raise_application_error(error_number, message[, {TRUE | FALSE}]); Error_number is a negative integer in the range Message is a character string up to 2048 bytes long

6 Advanced Exceptions Syntax of Raise_Application_Error
Pre-define procedure to return user-friendly message back to user

7 Database Programming Using Oracle 11g
Implementing Raise_application_error - I

8 Error Handling and Exceptions
declare name emp.ename%type; salary emp.sal%type; begin select ename, sal into name, salary from emp where empno=7369; if salary <5000 then Raise_application_error(-20030, ‘Invalid salary’); else

9 Error Handling and Exceptions
dbms_output.put_line('Valid Salary'); end if; exception when others then dbms_output.put_line(SQLERRM); end;

10 Database Programming Using Oracle 11g
Implementing User Defined Exception - II

11 Error Handling and Exceptions
Write a PL/SQL block to retrieve the ename, job, mrg and hiredate for a particular empno and make sure data is retrieved without error i-e if any field have null value then raise the following exceptions: Exception No Message -20010 No name -20020 No job 20030 No manager -20040 No hire date

12 Error Handling and Exceptions
declare v_ename emp.ename%TYPE; v_job emp.job%TYPE; v_mgr emp.mgr%TYPE; v_hiredate emp.hiredate%TYPE; p_empno emp.empno%type:=7654; BEGIN SELECT ename, job, mgr, hiredate INTO v_ename, v_job, v_mgr, v_hiredate FROM emp

13 Error Handling and Exceptions
WHERE empno = p_empno; IF v_ename IS NULL THEN RAISE_APPLICATION_ERROR(-20010, 'No name for ' || p_empno); END IF; IF v_job IS NULL THEN RAISE_APPLICATION_ERROR(-20020, 'No job for' || p_empno); IF v_mgr IS NULL THEN

14 Error Handling and Exceptions
RAISE_APPLICATION_ERROR(-20030, 'No manager for ' || p_empno); END IF; IF v_hiredate IS NULL THEN RAISE_APPLICATION_ERROR(-20040, 'No hire date for ' || p_empno); DBMS_OUTPUT.PUT_LINE('Employee ' || p_empno || ' validated without errors');

15 Error Handling and Exceptions
WHEN OTHERS THEN DBMS_OUTPUT.PUT_LINE('SQLCODE: ' || SQLCODE); DBMS_OUTPUT.PUT_LINE('SQLERRM: ' || SQLERRM); END;

16 Advanced Exceptions What is exception_init Pragma
Exception name can be associated with oracle exception number Ora – Error no can be intercepted and specific handler can be written

17 Advanced Exceptions What is exception_init Pragma
Pragma signifies that it’s a compiler directive Pragma is processed at compile time not run-time

18 Database Programming Using Oracle 11g
Syntax of Exception_init Pragma

19 Error Handling and Exceptions
DECLARE user_define_exception_name EXCEPTION; PRAGMA EXCEPTION_INIT(user_define_exception_name,-error_number); BEGIN statement(s); IF condition THEN RAISE user_define_exception_name; END IF; EXCEPTION WHEN user_define_exception_name THEN User defined statement (action) will be taken; END;

20 Advanced Exceptions Syntax of Exception_init Pragma
To associate customize message to pre-defined exceptions

21 Database Programming Using Oracle 11g
Implementing Exception_init Pragma -I

22 Error Handling and Exceptions
declare salary number; FOUND_NOTHING exception; Pragma exception_init(FOUND_NOTHING ,100); begin select sal in to salary from emp where ename =‘Akbar'; dbms_output.put_line(salary); exception WHEN FOUND_NOTHING THEN dbms_output.put_line(SQLERRM); end;

23 Database Programming Using Oracle 11g
Implementing Exception_init Pragma -II

24 Error Handling and Exceptions
DECLARE deadlock_detected EXCEPTION; PRAGMA EXCEPTION_INIT(deadlock_detected, -60); BEGIN dbms_output.put_line('In the begin section'); EXCEPTION WHEN deadlock_detected THEN dbms_output.put_line('Deadlock is detected'); END;


Download ppt "Database Programming Using Oracle 11g"

Similar presentations


Ads by Google