-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path4. Handling the Exceptions (Code Samples).html
85 lines (85 loc) · 3.49 KB
/
4. Handling the Exceptions (Code Samples).html
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
<pre class="prettyprint linenums">----------------- Handling the exception
declare
v_name varchar2(6);
begin
select first_name into v_name from employees where employee_id = 50;
dbms_output.put_line('Hello');
exception
when no_data_found then
dbms_output.put_line('There is no employee with the selected id');
end;
----------------- Handling multiple exceptions
declare
v_name varchar2(6);
v_department_name varchar2(100);
begin
select first_name into v_name from employees where employee_id = 100;
select department_id into v_department_name from employees where first_name = v_name;
dbms_output.put_line('Hello '|| v_name || '. Your department id is : '|| v_department_name );
exception
when no_data_found then
dbms_output.put_line('There is no employee with the selected id');
when too_many_rows then
dbms_output.put_line('There are more than one employees with the name '|| v_name);
dbms_output.put_line('Try with a different employee');
end;
----------------- when others then example
declare
v_name varchar2(6);
v_department_name varchar2(100);
begin
select first_name into v_name from employees where employee_id = 103;
select department_id into v_department_name from employees where first_name = v_name;
dbms_output.put_line('Hello '|| v_name || '. Your department id is : '|| v_department_name );
exception
when no_data_found then
dbms_output.put_line('There is no employee with the selected id');
when too_many_rows then
dbms_output.put_line('There are more than one employees with the name '|| v_name);
dbms_output.put_line('Try with a different employee');
when others then
dbms_output.put_line('An unexpected error happened. Connect with the programmer..');
end;
----------------- sqlerrm & sqlcode example
declare
v_name varchar2(6);
v_department_name varchar2(100);
begin
select first_name into v_name from employees where employee_id = 103;
select department_id into v_department_name from employees where first_name = v_name;
dbms_output.put_line('Hello '|| v_name || '. Your department id is : '|| v_department_name );
exception
when no_data_found then
dbms_output.put_line('There is no employee with the selected id');
when too_many_rows then
dbms_output.put_line('There are more than one employees with the name '|| v_name);
dbms_output.put_line('Try with a different employee');
when others then
dbms_output.put_line('An unexpected error happened. Connect with the programmer..');
dbms_output.put_line(sqlcode || ' ---> '|| sqlerrm);
end;
----------------- Inner block exception example
declare
v_name varchar2(6);
v_department_name varchar2(100);
begin
select first_name into v_name from employees where employee_id = 100;
begin
select department_id into v_department_name from employees where first_name = v_name;
exception
when too_many_rows then
v_department_name := 'Error in department_name';
end;
dbms_output.put_line('Hello '|| v_name || '. Your department id is : '|| v_department_name );
exception
when no_data_found then
dbms_output.put_line('There is no employee with the selected id');
when too_many_rows then
dbms_output.put_line('There are more than one employees with the name '|| v_name);
dbms_output.put_line('Try with a different employee');
when others then
dbms_output.put_line('An unexpected error happened. Connect with the programmer..');
dbms_output.put_line(sqlcode || ' ---> '|| sqlerrm);
end;
/
select * from employees where first_name = 'Steven';</pre>