Board index » cppbuilder » Re: Exception handling

Re: Exception handling


2005-06-14 05:03:47 AM
cppbuilder37
Quote
Please show your exact code. Have you tried this yet?

try {
IdHTTP1->Post("address", str1, str2);
}
catch(const Exception &e) {
ShowMessage(e.ClassName);
}

Yes, I tied.
Here is code:
void __fastcall TForm1::Button1Click(TObject *Sender)
{
TIdHTTP *IdHTTP1;
TStringStream *str1,*str2;
str1 = new TStringStream(NULL);
str2 = new TStringStream(NULL);
IdHTTP1 = new TIdHTTP(NULL);
IdHTTP1->Port = 80;
IdHTTP1->Host = "123.123.123.123";
try
{
ShowMessage("Before");
IdHTTP1->Post("123.123.123.123/Default.aspx",(TStream*)str1,(TStream*)str2);
ShowMessage("After");
}
catch(const Exception &e)
{
ShowMessage(e.ClassName);
}
}
Quote
running the project inside the IDE's de{*word*81}, you might just be seeing the
de{*word*81} catching exceptions that are internal to Indy which never actually
reach your code in the first place.
Exceptions that I want to catch are real exceptions (EIdSocketError with
message no route to host or EIdHTTPProtocolException with message 403
forbidden), and I think that they are not like exception "connection
closed gracefully". And when I run my program in IDE's de{*word*81}, I see
debug exception window with class name of exception and it's message,
and when I run it directly as standalone binary I see error window with
exception's message (for example massage with text "403 HTTP/1.1
Forbidden"). But still not able to catch it.
 
 

Re:Re: Exception handling

"Jan Slansk" < XXXX@XXXXX.COM >wrote in message
Quote
IdHTTP1->Port = 80;
IdHTTP1->Host = "123.123.123.123";
Get rid of those. They are useless in TIdHTTP as they get overwritten when
the URL is parsed.
Quote

IdHTTP1->Post("123.123.123.123/Default.aspx",(TStream*)str1,(TStre
am*)str2);
You are posting an empty input stream. That doesn't do you any good,
either, so you may as well not even use it at all:
IdHTTP1->Post("123.123.123.123/Default.aspx", str2);
Quote
Exceptions that I want to catch are real exceptions (EIdSocketError
with message no route to host or EIdHTTPProtocolException with
message 403 forbidden)
That is not what I meant. Of course they are real exceptions. But they may
be getting handled inside of Indy's internals and not propegating far enough
to reach your own code. The de{*word*81} always catches all exceptions
regardless of where they occur. If your last ShowMessage() is not being
called, then the exceptions are not actually reaching your own code, in
which case you can just ignore them since Indy is handling them internally
and your code will never see them when running outside of the de{*word*81}.
Quote
And when I run my program in IDE's de{*word*81}, I see debug exception
window with class name of exception and it's message
As well you should be. That is what the de{*word*81} is supposed to do. That
does not necessarily mean that the exceptions will reach your own code,
though.
Quote
and when I run it directly as standalone binary I see error window with
exception's message (for example massage with text "403 HTTP/1.1
Forbidden"). But still not able to catch it.
I highly doubt that. If Delphi can catch it, so can you. That is why I
told you to display the actual ClassName, so that you could see the actual
ClassName that is being thrown and can then update your catch statements
accordingly. Or, if you just catch Exception or EIdException directly and
not any specific derived classes, then you will be catching all possible
exceptions that can reach your own code.
Gambit
 

Re:Re: Exception handling

Quote
I highly doubt that. If Delphi can catch it, so can you. That is why I
told you to display the actual ClassName, so that you could see the actual
ClassName that is being thrown and can then update your catch statements
accordingly. Or, if you just catch Exception or EIdException directly and
not any specific derived classes, then you will be catching all possible
exceptions that can reach your own code.
OK, Let's talk about one exact exception to prevent speculations - 403
HTTP/1.1 Forbidden.
This is EdIdHTTPProtocolException. I doubt that this exception doesn't
reach my code. As you said,
I have seen that friend's Borland Delphi catch it, but my Borland c++
builder don't. I use catch(Exception &e)
catch(EIdException &e) and catch(EIdHTTPProtocolException &e) but
nothing works.
 

{smallsort}

Re:Re: Exception handling

"Jan Slansk" < XXXX@XXXXX.COM >wrote in message
Quote
OK, Let's talk about one exact exception to prevent speculations - 403
HTTP/1.1 Forbidden. This is EdIdHTTPProtocolException. I doubt that
this exception doesn't reach my code.
Yes, it will reach your code, and yes it is catchable in your own code.
Quote
I have seen that friend's Borland Delphi catch it, but my Borland c++
builder don't.
Again, I highly doubt that. I can catch it in my BCB without problems.
Quote
I use catch(Exception &e) catch(EIdException &e) and
catch(EIdHTTPProtocolException &e) but nothing works.
You should be catching by 'const' references, as I already showed you
earlier:
catch(const Exception &e)
catch(const EIdException &e)
catch(const EIdHTTPProtocolException &e)
Gambit
 

Re:Re: Exception handling

Quote
Again, I highly doubt that. I can catch it in my BCB without problems.
Can you tell me which version of BCB and Indy you are using ? And could you
show me code ? I agree with you that it is strange but my program really
don't catch it.
Quote
You should be catching by 'const' references, as I already showed you
earlier:

catch(const Exception &e)
catch(const EIdException &e)
catch(const EIdHTTPProtocolException &e)

Yes I use 'const' references, sorry I forgot write it in my last post.
 

Re:Re: Exception handling

"Jan Slansk" < XXXX@XXXXX.COM >wrote in message
Quote
Can you tell me which version of BCB and Indy you are using ?
Indy 9.0.50 in both BCB 5 and 6.
Quote
And could you show me code ?
I've already shown you the code.
Quote
I agree with you that it is strange but my program really don't catch it.
Do you have exception handling enabled in your Project Options?
Gambit
 

Re:Re: Exception handling

Quote
Do you have exception handling enabled in your Project Options?
Yes, I have.
 

Re:Re: Exception handling

I tried it on friend's BCB and it really works and exeptions are
catched. He has BCB6 and old Indy 6.
He has set same options as I have. Maybe something is crashed in my BCB
or... really don't know.
 

Re:Re: Exception handling

We have created a service oriented framework consisting of main app and
dlls, all built in Delphi 7. Services communicate with each other trough
interfaces. The problem is in exception handling. Module exceptions bypass
our application exception handler. Exceptions are of known type, but I
cannot figure out how to catch them all in one place.
Is there a way to integrate module exceptions in main application handler?
 

Re:Re: Exception handling

using D6, ADO, and microsoft sql db
would like to trap key violations so that a more user friendly message
can be displayed.
If i was using the BDE I think I might do something like this in the
posterror event
procedure TdmData.adodsEmpOTReqPostError(DataSet: TDataSet;
E: EDatabaseError; var Action: TDataAction);
begin
action := daAbort ;
if (E is EDBEngineError.Errors(0).ErrorCode = DBIERR_KeyViol) then
begin
// msg for key violation
end
else
begin
// unexpected error
raise;
end;
end;
As this is not the BDE, what is the comparable code for ado?
Where are the error codes defined?
2nd question..
Which method is preferred?
putting the exception code in the posterror event, or using a
try...except block around the actual post?
Thank you...
 

Re:Re: Exception handling

Terry,
I wrap my stuff in try, excepts. You can then loop through the Connection
objects errors collection. For more information on that you should check
into the ADO API Reference,
msdn.microsoft.com/library/default.asp
For example;
try
theCnn := GetDBConnection;
with theCnn do
begin
If not SetDBConnectionString(theCnn, srvr, db, iDbType, port)
then
begin
MessageDlg('Unable to set connection string to database!',
mtWarning, [mbOK], 0);
Exit;
end;
Connected := TRUE;
FConnectivityChecked := TRUE;
end;
except on E: Exception do
begin
for i := 0 to theCnn.Errors.Count - 1 do
strAdoErr := strAdoErr + theCnn.Errors[i].Description +
#13#10;
MessageDlg('A critical system failure has occured trying to
connect ' +
'to the following Server ' + srvr + ' and Database ' + db +
'.' + #13#10 +
'ADO Error: ' + strAdoErr + #13#10 +
'System Error: ' + E.Message + #13#10 +
'The application will close now!', mtError, [mbOK], 0);
Application.Terminate;
end;
end; // of try/except
hth,
brian
--
Got a big event coming up? Let us
help coordinate your event. For more
visit www.kissemgoodbye.com
"Terry" < XXXX@XXXXX.COM >wrote in message
Quote
using D6, ADO, and microsoft sql db

would like to trap key violations so that a more user friendly message
can be displayed.

If i was using the BDE I think I might do something like this in the
posterror event

procedure TdmData.adodsEmpOTReqPostError(DataSet: TDataSet;
E: EDatabaseError; var Action: TDataAction);
begin
action := daAbort ;
if (E is EDBEngineError.Errors(0).ErrorCode = DBIERR_KeyViol) then
begin
// msg for key violation
end

else
begin
// unexpected error
raise;

end;

end;

As this is not the BDE, what is the comparable code for ado?
Where are the error codes defined?

2nd question..
Which method is preferred?
putting the exception code in the posterror event, or using a
try...except block around the actual post?

Thank you...