rules repository

G-2160 Avoid initializing variables using functions in the declaration section.

Severity Warning Check Free

Reason

If your initialization fails, you will not be able to handle the error in your exceptions block.

Example

Non-Compliant Example

declare
   co_department_id  constant integer := 100;
   l_department_name departments.department_name%type :=
      department_api.name_by_id(in_id => co_department_id);
begin
   sys.dbms_output.put_line(l_department_name);
end;
/
Issues
LineColumnMessage
47Function call in declare section.

Compliant Solution - ★★★★★

declare
   co_department_id  constant integer                          := 100;
   co_unkown_name    constant departments.department_name%type := 'unknown';
   l_department_name departments.department_name%type;
begin
   <<init>>
   begin
      l_department_name := department_api.name_by_id(in_id => co_department_id);
   exception
      when value_error then
         l_department_name := co_unkown_name;
   end init;

   sys.dbms_output.put_line(l_department_name);
end;
/

References