G-5020
Warning
Never handle unnamed exceptions using the error number.
Reason
When literals are used for error numbers the reader needs the error message manual to unterstand what is going on. Commenting the code or using constants is an option, but it is better to use named exceptions instead, because it ensures a certain level of consistency which makes maintenance easier.
Example
Non-Compliant Example
declare
co_no_data_found constant integer := -1;
begin
my_package.some_processing(); -- some code which raises an exception
exception
when too_many_rows then
my_package.some_further_processing();
when others then
if sqlcode = co_no_data_found then
null;
end if;
end;
/Issues
| Line | Column | Message |
|---|---|---|
| 9 | 10 |
Compliant Solution - ★★★★★
begin
my_package.some_processing(); -- some code which raises an exception
exception
when too_many_rows then
my_package.some_further_processing();
when no_data_found then
null; -- handle no_data_found
end;
/References
- same as Trivadis G-5020
- similar to plsql:WhenOthersAsOnlyExceptionHandlerCheck
The scope of plsql:WhenOthersAsOnlyExceptionHandlerCheck is every WHEN OTHERS handler and not the use of SQLCODE within an error handler.