Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Help: Trapping Oracle Errors? 1

Status
Not open for further replies.

JediDan

Programmer
May 15, 2002
128
US
Hello,

I would like to know how I can trap errors being returned from an Oracle database, such as invalid database name errors, invalid passwords, etc.

Any help is appreciated.

Thanks.
 
Hi -

I suggest that you enclose the code that may generate errors inside a try..except loop - then check the Exception type.

So let say you have a connection routine when an App starts - do something like ..

Try
Database1.connect;

except

on E:Exception do

showmessage(E.Classtype)

end;


This is for test purposes only and will allow you to see exactly the Type of Exception being generated. Once you know this - you can specifically trap these errors.


Then do the following to trap the specific error code generated :

try

Database1.connect;

Except

on E:Exception do

begin

if E.ClassType = EIBInterbaseError then
begin
Showmessage('Specific error code generated : ' + IntTOStr(EIBInterbaseError(E).SQLCode));

end;

end;



Please note that I have used EIBInterbaseError here - but this is just for example purposes. The theory remains the same though - you just need to insert the correct Exception type based on your circumstances. Once you have the SQLCode you can trap this.

Hope this helps..

Opp.

 
Thanks, that got me started in the right direction. Does it matter that I'm using ADO for my connections?
 
No JediMan -

The only thing that will differ is the "Type" of exception being reported - thats why you use the first bit of code (above) to determine this first. After that you can trap for that specific type and in fact specific errors of that type (2nd bit of code.)

So eventually code will look something like this ..


try

Database1.connect;

Except

on E:Exception do

begin

if E.ClassType = EIBInterbaseError then
(E).SQLCode) = -512 then
// Specific actions here
;
end;

end;



The above code would work for an Interbase error (trap a referential integrity conflict I think .) Yours may differ in the type - so might be "EADODatabaseError" or something like that, and the specific numbers vary (-512 in the above example.)

Opp.
 
One thing that happened when I tried your first code sample, specifically:
Code:
ShowMessage(E.ClassType)
the compiler gave me: incompatible types: String and TClass. So I changed the code to:
Code:
ShowMessage(E.ClassName)
and an error message came up with EOleException.
But when I tried this in the section code fragment, as
Code:
if E.ClassType = EOleException...
It came back with "Undeclared identifier: EOleException"

So I am stuck now...
 
Yes - my bad about the ClassType - it should have been Classname as you discovered. As for the solution to the latest problem - simple add ComObj to the uses clause and this error will not be generated.

Opp.
 
Ok, I hate to be a pain because you're being quite helpful.

I got rid of all the errors, but the window that comes up telling me it's an EOleException comes up even if I comment-out all of the try-except code.

So it doesn't look like it's actually catching the error...

Thanks again
 
OK - this is to do with the Delphi environment. Go to the "options" menu and select "Debugger Options" - then go to "language Exceptions" and un-tick "Stop on Delphi Exceptions".

That will stop the exception being raised in the IDE.

Opp.
 
So I was doing all of this stuff the entire time on a recordset.open, not my connection.open...

I do now get the proper message box saying only: EOleException, as you had first described.

However, EOleException does not have a .SQLCode property or even anything similar. I can't find any properties besides .ClassName and .Message. I need to get to the ORA-XXXXX error numbers so I can really start trapping stuff.

Any thoughts? Thanks again for your help.
 
The best thing to do is use the Delphi help file realating to EOleException. You will find there may not be an SQLError code but there is an ErrorCode that you can check.

Opp.
 
I'm sure you're right, but I can't seem to find anything related to error number, or error value, etc. It's almost as if EOleException is a general exception type that has to be "peeled back" before you can get to the real stuff, there's just not many methods assigned to it.

I really do appreciate your help, though. I'm going to start a new thread on EOleException and see if I can't dredge up some more info.

Thanks again!
 
JediMan - try this (you need to cast it to the correct type before it would worlk.)



try
Except
on E:Exception do
if E.ClassType = EOleException then
Showmessage(IntTOStr(EOleException(E).ErrorCode));
end;


This should give you the error code. But you can see I had to cast the Exception to the right type before I could access the property in question. As in

EOLEExeption(E).ErrorCode

Opp.

P.S Dont forget to include ComObj in uses clause.


 
Wow... I swore I tried that before, but this time it worked.

It doesn't return the Oracle error number, but I think I can still use this.

Thanks again!
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top