Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add ability for throws annotation to accept variables #685

Closed
lwasylow opened this issue May 30, 2018 · 1 comment
Closed

Add ability for throws annotation to accept variables #685

lwasylow opened this issue May 30, 2018 · 1 comment
Assignees
Milestone

Comments

@lwasylow
Copy link
Member

Would be good if the throws annotation allow to instead of hardocode number use a constant from other packages. Quite often the error numbers are defined in common package so that can be reused.
eg. - - %throws(schema.package.c_e_exception_no)

@jgebal
Copy link
Member

jgebal commented May 30, 2018

There are in general 4 ways to pass an exception:

  • By exception number
--%throws(-20000)
procedure test_that_raises_20000;
  • by exception number variable
package my_exceptions is
  e_some_exception constant integer := -20000;
end;
/
--%throws(my_exceptions.e_some_exception_no)
procedure test_that_raises_20000;
  • By exception variable
package my_exceptions is
  e_some_exception exception;
  pragma exception_init(e_some_exception, -20000);
end;
/
--%throws(my_exceptions.e_some_exception)
procedure test_that_raises_20000;
  • by named exception
--%throws(NO_DATA_FOUND)
procedure test_that_raises_20000;

If we want to support variables, we should probably support both kinds (number/exception)
If we will go into effort of supporting exception type, we're actually already there to support named exceptions too.

Sample code:

create or replace package my_exceptions is
  e_some_exception exception;
  pragma exception_init(e_some_exception, -20000);
  e_other_exception constant integer := -20000;
end;
/

declare
    procedure check_var(a_var varchar2) is
      a varchar2(250);
      b varchar2(250);
      c varchar2(250);
      dblink varchar2(250);
      next_pos pls_integer;
    begin
      --check if it is a number first
      dbms_utility.name_tokenize(a_var, a, b, c, dblink, next_pos);
      --check if it is a predefined exception
      begin
        execute immediate 'begin null; exception when '||a_var||' then null; end;';
        dbms_output.put_line(a_var||' is a named exception');
      exception
        when others then
          if dbms_utility.format_error_stack() like '%PLS-00485%' then
            execute immediate 'declare x positiven := -('||a_var||'); begin null; end;';
            dbms_output.put_line(a_var||' is a negative number');
            --- then what?
          end if;
      end;
    end;
begin
  check_var('my_exceptions.e_some_exception');
  check_var('my_exceptions.e_other_exception');
  check_var('no_data_found');
--  check_var('-20000');
end;
/

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants