You exception handler
when others then
dbms_output.put_line(' Selecting tracking Id');
utl_file.putf(f_errors, 'Select Tracking ID :'||v_tracking_id
||':'||sqlerrm);
rollback;
return;
contains error tracking write to file - what about dbms_output.put_line() with the same information ? You could get this as spool file output. Also seems you use loop - end loop; and goto control method - could you use while() looping instead ? It might be more transaparent.
Ald also you have a problem with RETURN statement I guess - because you don't close file handler before return in nested block you don't have information about the error, the file is empty and you don't reach the end of main block:
SQL> declare
2
3 f_errors utl_file.file_type;
4
5 begin
6 utl_file.fclose(f_errors);
7 f_errors := utl_file.fopen('FDC_BTI_DIR', 'test_01.txt', 'w');
8 begin
9
10 raise no_data_found;
11
12 exception
13 when others then
14 dbms_output.put_line(' Selecting tracking Id');
15 utl_file.putf(f_errors, 'Select Tracking ID :'||sqlerrm);
16 rollback;
17 return;
18 end;
19 utl_file.fclose(f_errors);
20 end;
21 /
Selecting tracking Id
SQL> declare
2
3 f_errors utl_file.file_type;
4 buff varchar2(4000);
5
6 begin
7 utl_file.fclose(f_errors);
8 f_errors := utl_file.fopen('FDC_BTI_DIR', 'test_01.txt', 'r');
9 utl_file.get_line(f_errors, buff, 4000);
10 dbms_output.put_line(buff);
11 utl_file.fclose(f_errors);
12 end;
13 /
declare
*
error in line 1:
ORA-01403: data not found
ORA-06512: at "SYS.UTL_FILE", line 106
ORA-06512: at "SYS.UTL_FILE", line 746
ORA-06512: at line 9
SQL> declare
2
3 f_errors utl_file.file_type;
4
5 begin
6 utl_file.fclose(f_errors);
7 f_errors := utl_file.fopen('FDC_BTI_DIR', 'test_01.txt', 'w');
8 begin
9
10 raise too_many_rows;
11
12 exception
13 when others then
14 dbms_output.put_line(' Selecting tracking Id');
15 utl_file.putf(f_errors, 'Select Tracking ID :'||sqlerrm);
16 utl_file.fclose(f_errors);
17 rollback;
18 return;
19 end;
20 utl_file.fclose(f_errors);
21 end;
22 /
Selecting tracking Id
SQL> declare
2
3 f_errors utl_file.file_type;
4 buff varchar2(4000);
5
6 begin
7 utl_file.fclose(f_errors);
8 f_errors := utl_file.fopen('FDC_BTI_DIR', 'test_01.txt', 'r');
9 utl_file.get_line(f_errors, buff, 4000);
10 dbms_output.put_line(buff);
11 utl_file.fclose(f_errors);
12 end;
13 /
Select Tracking ID :ORA-01422: exact fetch returns more than requested number of rows
SQL> declare
2
3 f_errors utl_file.file_type;
4
5 begin
6 utl_file.fclose(f_errors);
7 f_errors := utl_file.fopen('FDC_BTI_DIR', 'test_01.txt', 'w');
8 begin
9
10 raise invalid_number;
11
12 exception
13 when others then
14 dbms_output.put_line(' Selecting tracking Id');
15 utl_file.putf(f_errors, 'Select Tracking ID :'||sqlerrm);
16 rollback;
17 end;
18 utl_file.fclose(f_errors);
19 end;
20 /
Selecting tracking Id
SQL> declare
2
3 f_errors utl_file.file_type;
4 buff varchar2(4000);
5
6 begin
7 utl_file.fclose(f_errors);
8 f_errors := utl_file.fopen('FDC_BTI_DIR', 'test_01.txt', 'r');
9 utl_file.get_line(f_errors, buff, 4000);
10 dbms_output.put_line(buff);
11 utl_file.fclose(f_errors);
12 end;
13 /
Select Tracking ID :ORA-01722: invalid_number