Skip to Main Content
  • Questions
  • Best practices for java/oracle interaction wrt exceptions

Breadcrumb

Question and Answer

Tom Kyte

Thanks for the question.

Asked: August 04, 2005 - 11:23 am UTC

Last updated: May 08, 2007 - 10:29 am UTC

Version: 9.2.1

Viewed 1000+ times

You Asked

Hi Tom,

huge fan. I am hoping you can help with some tricky, almost philosophical issues I have been having recently.

I am a Java programmer primarily but used to code PL/SQL a while back and currently enjoying doing both. Seeing both side of the fence means I tend to think things through (unlike my collegues!).

My question is this, how best to deal with exceptions from the oracle system? The environment I'm in mandates that all db interaction is via stored procs. The current practice is to use return codes in the pl/sql that is return via out parameters to the java.

However, each proc/function can possibly return 1 (success), -1 (unknown failure) or 2,3,4,etc which is specific to that proc.

I'm finding this not very helpful becuase alot of these codes do not help me as a java developer as i cannot recover from them. My java code is becoming a mess of switch staements as i check the codes.

Do you think an approach where only recoverable errors are checked for with valid return codes and let the rest go back as a general oracle exception (and bubble up to the top error page in the web app) is a better approach???

Do you think we should standardise on unique codes across the app instead? I have seen some code from SteveF where he maintains an array of error codes and descriptions with a package wrapper. I could aslo centralise the error codes in the java side so i can keep the code in one place rather than all over my DAOs?

Any advice you can give in this area is appreciated.

Thanks

RP

and Tom said...

The
current practice is to use return codes in the pl/sql that is return via out
parameters to the java.


Ugh, return codes, how 1980's. Tell me, do the java programmers catch ALL exceptions at their lowest level and turn them into return codes?!?


Just like in java, ANYTHING that cannot be dealt with in plsql (eg: it wasn't really an error, it was an EXPECTED exception -- no_data_found is like that, maybe it is OK to not find any data) should be propagated back to the client, the client can do what it likes with it at that point -- log it, send it to YAF (yet another function) that logs it and converts it into "whatever", but the client should get something they physically have to CATCH and deal with (else you know what happens? people forget to check return codes and silently IGNORE ERRORS)

I know this will spark a debate, This runs counter to some others (plsql programmers), but I truly believe that unless you can DEAL with the exception, you let it propagate to the top level, until there is something that CAN deal with it -- and then they do.


It is like "when others", I don't like it because it gets abused, people use it to "silently, but by accident, HIDE ERRORS and erroneous program execution is the silent side effect".

Rating

  (9 ratings)

Is this answer out of date? If it is, please let us know via a Comment

Comments

Parsing the exception returned

A reader, August 05, 2005 - 4:30 am UTC

You can also parse the error message in your Java code.
I have already seen such a code sample.
It can show constraint violations.
I already experimented to create an error stack using Oracle objects (type) and pass it back to Java.

Methodology for error trapping is not clear

Aditya Saraogi, August 05, 2005 - 7:44 am UTC

Hi Tom,

You suggest that the error should be allowed to escalate to the highest calling point where it can be handled. My question is even if the error propagates to that calling point, what happens in that program. I mean, I have an Oracle Error Number with me,what am I supposed to do with it? I will have to write nested/multiple catch statements that can trap every possible type of error and deal with every possible failure scenario for every single procedure.

All this is necessary if I dont wish to use the notorious WHEN OTHERS statement. What is the sane limit for checking errors?

What could possibly be the work around for such a scenario?

Thanks
Aditya Saraogi

Tom Kyte
August 05, 2005 - 11:26 am UTC

you could in turn call that routine that converts the error into something "meaningful to YOUR program". But let the error propagate AS AN ERROR to the client, something "un-ignoreable"


You CANNOT write mutliple catch statements -- for every procedure could return EVERY error (and that is the point here - they are *errors*, unexpected exceptions that NO ONE ANTICIPATED really)

Error Propogation All the Way Up

Kevin Shidler, August 11, 2005 - 3:14 pm UTC

I am a fan of error propogation all-the-way up to the client tier, especially for "unknown errors". They are unknown (similar to ORA-00600 errors). Someone needs to know about them. Certain errors are not considered during application design, but by propogating the error up to the client, during the testing phase of application design, the tester will notify the necessary personnel, and the change will be made. It would be impossible to handle every single error that can occur while creating any application. So why hide it in a clause similar to:

EXCEPTION
WHEN OTHERS THEN
DBMS_OUTPUT.PUT_LINE('AN ERROR OCCURRED: '||
SUBSTR(SQLERRM,1,200);
END;

Notice the absence of "RAISE". We have effectively handled the error.

I feel like it is re-inventing the ERROR object by returning codes that represent errors, unless those errors are business-rule defined errors for the application. Just propogate the error object back up to whomever the caller is. If you can find a graceful way of handling the error (defined by business rules), then in that situation, you probably don't need to propogate the error once it has been handled. It is all application-dependent based upon the business rules/requirements.


Tom Kyte
August 12, 2005 - 8:13 am UTC

you have my vote.

if the client doesn't understand the error in production, it should say something like:


"I am sorry, we hit something unexpected, I have invoked the generic log a message and send an email to the support team with all of the information about this error. You may contact the help desk with id = 42525325 to gather more information about the solution for this problem"



Exceptions stored in data dictionary

Vijay, May 02, 2007 - 5:30 am UTC

Dear Tom,
good day to you as always, just out of curiosity wanted to know are the Oracle exception stored in any table I mean any dictionary object of Oracle.

Thanks in advance for your time on this query.

Regards,
Vijay'S
Tom Kyte
May 02, 2007 - 9:02 am UTC

if you mean error messages (exceptions being a PLSQL programming construct), we can get access to them:

ps$tkyte%ORA10GR2> exec dbms_output.put_line( sqlerrm( -1 ) );
ORA-00001: unique constraint (.) violated

PL/SQL procedure successfully completed.

some more details on this

VijayS, May 02, 2007 - 9:28 am UTC

Dear Tom,
thanks for taking out your time on this, what I intended to know is, from where is the message and error number extracted, is there any table which holds all the error number and messages, if yes which one.

Thanks again for your time on this.

Regards,
Vijay'S
Tom Kyte
May 02, 2007 - 5:04 pm UTC

I showed you how to do this, there is no table, they are stored in the file system, this function provides access to it.

table

A reader, May 02, 2007 - 10:26 am UTC

create table temp(i integer, errm varchar2(4000));

declare x varchar2(4000);
begin
for i in -99999..0 loop
x:=sqlerrm(i);
insert into temp values(i, x);
end loop;
commit;
end;
/

select * from temp
where errm not like '%non-ORACLE exception%'
and errm not like 'ORA-%Message % not found; product=RDBMS; facility=ORA'

BUT user defined exceptions

A reader, May 02, 2007 - 11:11 am UTC

thanks for the time on this

VijayS, May 06, 2007 - 1:02 pm UTC

Dear Tom,
good day to you as always, thanks for your reply on this, I just wanted to confirm that the exceptions are not stored as such in any tables of the database.

Regards,
VijayS
Tom Kyte
May 08, 2007 - 10:29 am UTC

no, the error messages are in a file on the server.

pl/sql error propagation to java

Lawrence Raymond, August 11, 2014 - 7:22 pm UTC

Propagating pl/sql excepytions back to the [java] caller has to be the way to go.

As DBA, I rec. using an EXCEPTION WHEN OTHERS section: call an 'async' procedure to write the error message, with identifying info., to an oracle table. Then, do: RAISE.

More to Explore

PL/SQL demos

Check out more PL/SQL tutorials on our LiveSQL tool.

PL/SQL docs

PL/SQL reference manual from the Oracle documentation library