-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path17. Handling the Exceptions in Subprograms (Code Samples).html
More file actions
53 lines (53 loc) · 2.05 KB
/
17. Handling the Exceptions in Subprograms (Code Samples).html
File metadata and controls
53 lines (53 loc) · 2.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
<pre class="prettyprint linenums">----------------- An unhandled exception in function
create or replace function get_emp(emp_num employees.employee_id%type) return employees%rowtype is
emp employees%rowtype;
begin
select * into emp from employees where employee_id = emp_num;
return emp;
end;
----------------- calling that function in an anonymous block
declare
v_emp employees%rowtype;
begin
dbms_output.put_line('Fetching the employee data!..');
v_emp := get_emp(10);
dbms_output.put_line('Some information of the employee are : ');
dbms_output.put_line('The name of the employee is : '|| v_emp.first_name);
dbms_output.put_line('The email of the employee is : '|| v_emp.email);
dbms_output.put_line('The salary of the employee is : '|| v_emp.salary);
end;
----------------- hanling the exception wihout the return clause - not working
create or replace function get_emp(emp_num employees.employee_id%type) return employees%rowtype is
emp employees%rowtype;
begin
select * into emp from employees where employee_id = emp_num;
return emp;
exception
when no_data_found then
dbms_output.put_line('There is no employee with the id '|| emp_num);
end;
----------------- handling and raising the exception
create or replace function get_emp(emp_num employees.employee_id%type) return employees%rowtype is
emp employees%rowtype;
begin
select * into emp from employees where employee_id = emp_num;
return emp;
exception
when no_data_found then
dbms_output.put_line('There is no employee with the id '|| emp_num);
raise no_data_found;
end;
----------------- handling all possible exception cases
create or replace function get_emp(emp_num employees.employee_id%type) return employees%rowtype is
emp employees%rowtype;
begin
select * into emp from employees where employee_id = emp_num;
return emp;
exception
when no_data_found then
dbms_output.put_line('There is no employee with the id '|| emp_num);
raise no_data_found;
when others then
dbms_output.put_line('Something unexpected happened!.');
return null;
end;</pre>