2

I'm writing my pl sql package. I have a function like

FUNCTION my_func(argument IN NUMBER)
        RETURN NUMBER
BEGIN
   return some_package.somefunction(argument);
END;

I want to handle exceptions, that are thrown if

  1. some_package doesnt exist.
  2. somefunction doesnt exist.

What are those exceptions? Are they predefined or should I define them to handle? I'm not sure they are in list here in predefined ones.

And how can I handle every exception?

4
  • how would you handle the function not existing? Let's not go down the path of returning 0 for failure and every other number means success, that way leads to disaster Commented Feb 11, 2016 at 16:14
  • 3
    If the called function does not exist, you'll probably have errors at compile-time, not run-time errors to handle. Commented Feb 11, 2016 at 16:17
  • 1
    You can't. You're talking about compilation errors not runtime errors. If the package doesn't exist, your function can't be compiled. If it can't be compiled, it can't be executed. If it can't be executed, you can't have an exception handler block that catches the error. I suppose if you were a masochist you could write your code so that you use dynamic SQL everywhere so that compilation errors become runtime errors. But that is a terrible way to write software. It generally doesn't make sense to handle every error-- handle those errors you expect and let the rest propagate up. Commented Feb 11, 2016 at 17:15
  • Ok, thank you all. I understand now that it is not runtime exception and cant be handled. Commented Feb 11, 2016 at 17:25

2 Answers 2

1

To answer your general question, most Oracle errors don't have pre-defined named exceptions associated with them, just the couple of dozen you found.

However, you can create your own named exceptions and associate them like this:

declare
    table_or_view_does_not_exist exception;
    pragma exception_init (table_or_view_does_not_exist, -942);
    l_table_name varchar2(30) := 'XXX';
    l_count integer;
begin
    execute immediate 'select count(*) from ' || l_table_name into l_count;
exception
    when table_or_view_does_not_exist then
        raise_application_error (-20001, 'No such table exists');
end;
/

declare
*
ERROR at line 1:
ORA-20001: No such table exists
ORA-06512: at line 10

How you actually handle the exception is up to you.

You can declare such exceptions in a package specification, and then use them in other code:

when my_package.my_exception then...

Alternatively, you can catch them all in WHEN OTHERS and then use sqlcode to identify them:

declare
    l_table_name varchar2(30) := 'XXX';
    l_count integer;
begin
    execute immediate 'select count(*) from ' || l_table_name into l_count;
exception
    when others then
        case sqlcode
            when -942 then
                raise_application_error (-20001, 'No such table exists');
            else
                raise;
        end case;
end;
Sign up to request clarification or add additional context in comments.

1 Comment

Does this piece when others then of code handle every possible exception?
0

One thing is sure, "some_package" AND "some_package.somefunction" existed when you created the function "my_func" otherwise you would have had an error upon creation.

If they are removed (either the package or the function in the package) then "my_func" will automatically become invalid and you won't even be able to execute it.

Test case :

create or replace package some_package as

function somefunction(a number) return number;

end;
/

create or replace package body some_package as

function somefunction(a number) return number is
begin
  return 1;
end;

end;
/

create or replace function my_func return number is
begin
  return some_package.somefunction(1);
end;
/

set serveroutput on size 2000

begin
  dbms_output.put_line(my_func());
end;
/

drop package some_package;

begin
  dbms_output.put_line(my_func());
end;
/

Yields :

Package created.

Package body created.

Function created.

1

PL/SQL procedure successfully completed.

Package dropped.

dbms_output.put_line(my_func()); * ERROR at line 2: ORA-06550: line 2, column 24: PLS-00905: object DEMO.MY_FUNC is invalid ORA-06550: line 2, column 3: PL/SQL: Statement ignored

The only way to prevent "my_func" from becoming invalid is to call the function using an "execute immediate" and then trap the error.

New "my_func" would look like this and would NEVER become invalid.

create or replace function my_func return number is
  v_return     number;
begin
  execute immediate 'begin :r := some_package.somefunction(1); end;' using out v_return;
  return v_return;
exception
  when others then
    dbms_output.put_line(SQLERRM);
    dbms_output.put_line('Package or function does not exists anymore');
    return -1;
end;
/

And would yield this result :

Package created.


Package body created.


Function created.

1

PL/SQL procedure successfully completed.


Package dropped.

ORA-06550: line 1, column 13:
PLS-00201: identifier 'SOME_PACKAGE.SOMEFUNCTION'
must be declared
ORA-06550: line 1, column 7:
PL/SQL: Statement ignored
Package or function does not exists anymore
-1

PL/SQL procedure successfully completed.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.