Oracle exception information

2.1k views Asked by At

I'm wondering if there is a way to get a little more information about what caused the exception:

Error starting at line 5 in command:
exec ....
Error report:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at ..., line 558
ORA-06512: at ..., line 752
ORA-06512: at line 1
06502. 00000 -  "PL/SQL: numeric or value error%s"
*Cause:    
*Action:

I'm assuming this means I have a variable that is too small for what I tried to put in it... But why can't Oracle tell me what variable this is? It would be great for both debugging and production support people to be able to say to our client I think you have this field too big and that's what caused the error, rather than just "some value was too big and we really have no idea which one"... Using the line number would required someone who understood sql and looking at the code which is not ideal.

Why is there nothing under the Cause and Action fields?

When you try to insert into a col with data that is too large it tells you what col... I would like similar information here.

Is that possible without having to put an exception handler after every plsql line of code?

1

There are 1 answers

2
BQ. On

Oracle can't know what your intent is with the error information. Knowing the variable name you're storing it as or where exactly it came from isn't necessarily in the best interests of the end user or security.

For example, I can generate your error easily:

SQL> declare
  2  v_tooshort varchar2(3);
  3  begin
  4    select 'too long' into v_tooshort from dual;
  5  end;
  6  /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 4

The error already gives the line number.

Would you prefer the error give the variable name (v_tooshort)? That's not helpful to a user.

Is the right information the value, "too long"? Or the fact that it's a dummy column from the dual table? (or an actual column and table)?

Because the error arises from a select into instead of an insert statement, it's not like there's a specific database constraint the exception can identify by name.

EDIT (to address issue raised in a comment): That's not true. You'll get the column name and lengths returned when doing an insert (as an ORA-12899), but not when doing a select into even if it's using data from a table:

SQL> create table test_length (tooshort varchar2(3));

Table created.

SQL> begin
  2    insert into test_length(tooshort) values ('too long');
  3  end;
  4  /
begin
*
ERROR at line 1:
ORA-12899: value too large for column "MYUSER"."TEST_LENGTH"."TOOSHORT"
(actual: 8, maximum: 3)
ORA-06512: at line 2

SQL> insert into test_length(tooshort) values ('abc');

1 row created.

SQL> commit;

Commit complete.

SQL> declare
  2    v_onechar varchar2(1);
  3  begin
  4    select tooshort into v_onechar from test_length;
  5  end;
  6  /
declare
*
ERROR at line 1:
ORA-06502: PL/SQL: numeric or value error: character string buffer too small
ORA-06512: at line 4

EDIT 2:

You can nest your select into into its own begin-exception-end block and raise whatever error you like (providing a unique error number and descriptive error text):

SQL> ed
Wrote file afiedt.buf

  1  declare
  2    v_onechar varchar2(1);
  3  begin
  4    select tooshort into v_onechar from test_length;
  5  exception
  6    when value_error then
  7      RAISE_APPLICATION_ERROR(-20011, 'my variable named v_onechar is too short for the data returned from test_lengt
h.tooshort');
  8* end;
SQL> /
declare
*
ERROR at line 1:
ORA-20011: my variable named v_onechar is too short for the data returned from
test_length.tooshort
ORA-06512: at line 7