Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: Exception reaching catch all, but I don't want it to



Permlink Replies: 4 - Last Post: Oct 28, 2016 1:44 PM Last Post By: Colin Maharaj
Colin Maharaj

Posts: 122
Registered: 12/2/99
Exception reaching catch all, but I don't want it to
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 23, 2016 6:22 PM
Hi all,
I had some issues with exception handling and was able to do some work
to rectify. I really thought I would get root causes of error in the
Socket process, but I am still getting catch all occurring.
Below is the basic code.

AnsiString A;
try
{
TCPClient1->IOHandler->WriteLn("SEND");
strcpy(Buff, "x");
bool r = TCPClient2->Socket->Readable(5000);
if (r == true)
{
A = TCPClient2->IOHandler->ReadLn("\n");
}
// other things with the data....
}

catch (EIdConnectException & E)
{
A = E.Message;
}

catch (EIdSocketError & E)
{
int e = E.LastError;
}

catch (EIdException & E)
{
A = E.Message;
if (E.Message == "Connection Closed Gracefully.")
{
TCPClient1->IOHandler->CloseGracefully();
}
}

catch (...)
{
A = "Unknown exception...";
if (TCPClient1->IOHandler)
{
TCPClient1->IOHandler->CloseGracefully();
}
Sleep(100);
}

Martin van der ...

Posts: 57
Registered: 7/14/02
Re: Exception reaching catch all, but I don't want it to
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 23, 2016 11:41 PM   in response to: Colin Maharaj in response to: Colin Maharaj
If you want to find out where the exception is coming from and the debugger isn't showing you, you can try to also catch type Exception. You'll at least get a message that's sometimes useful. Assuming it's a VCL exception... If not, try catching std::exception.

catch (Exception & E)
{
A = E.Message;
}

catch (std::exception & E)
{
A = E.what();
}
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Exception reaching catch all, but I don't want it to
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 24, 2016 12:15 PM   in response to: Martin van der ... in response to: Martin van der ...
Martin wrote:

If not, try catching std::exception.

That only works for exceptions that are thrown by the STL.

--
Remy Lebeau (TeamB)
Remy Lebeau (Te...


Posts: 9,447
Registered: 12/23/01
Re: Exception reaching catch all, but I don't want it to
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 24, 2016 12:14 PM   in response to: Colin Maharaj in response to: Colin Maharaj
Colin wrote:

bool r = TCPClient2->Socket->Readable(5000);

You don't really need to call Readable() directly. If you use the ReadTimeout
property instead, or even the optional ATimeout parameter of ReadLn(), you
can just call ReadLn() by itself and let it raise an EIdReadTimeout exception
if it times out.

catch (EIdConnectException & E)

EIdConnectException is one of several exceptions that TIdTCPClient::Connect()
may throw. But you are not calling Connect() inside your try block, so why
are you expecting EIdConnectException to be catchable?

catch (EIdSocketError & E)
catch (EIdException & E)

Those are two possible exceptions that can be thrown in your try block.
There are others...

if (E.Message == "Connection Closed Gracefully.")

That error message belongs to the EIdConnClosedGracefully exception. So
either write a separate catch() block for EIdConnClosedGracefully directly,
or else yoou can use dynamic_cast to check whether or not your caught EIdException
is actually an EIdConnClosedGracefully.

TCPClient1->IOHandler->CloseGracefully();

You should not be calling CloseGracefully() manually. All that does is sets
the IOHandler's ClosedGracefully property to true, which has already been
done before the EIdConnClosedGracefully exception is thrown. If you are
actually trying to close the socket, you should be calling Disconnect() instead.
Which you should call anyway if you get any socket-related exception,
since you don't know the state of the socket anymore.

catch (...)

You are missing a catch() block for non-Indy VCL Exception types.

Try something more like this insted:

System::String A;
try
{
    TCPClient1->IOHandler->WriteLn("SEND");
    strcpy(Buff, "x");
    A = TCPClient2->IOHandler->ReadLn("\n", 5000);
    // other things with the data....
}
/*
catch (const EIdConnectException & E)
{
    A = E.Message;
}
*/
catch (const EIdSocketError & E)
{
    A = E.Message;
    int e = E.LastError;
}
catch (const EIdConnClosedGracefully &E)
{
    A = E.Message;
    TCPClient1->Disconnect();
}
catch (const EIdException & E)
{
    A = E.Message;
}
catch (const Exception &E)
{
    A = E.Message;
    TCPClient1->Disconnect();
    Sleep(100);
}
catch (...)
{
    A = "Unknown exception...";
    TCPClient1->Disconnect();
    Sleep(100);
}


Alternatively:

System::String A;
try
{
    TCPClient1->IOHandler->WriteLn("SEND");
    strcpy(Buff, "x");
    A = TCPClient2->IOHandler->ReadLn("\n", 5000);
    // other things with the data....
}
catch (const Exception &E)
{
    A = E.Message;
    TCPClient1->Disconnect();
 
    const EIdSocketError *se = dynamic_cast<const EIdSocketError*>(&E);
    if (se)
    {
        int e = se->LastError;
    }
 
    Sleep(100);
}
catch (...)
{
    A = "Unknown exception...";
    TCPClient1->Disconnect();
    Sleep(100);
}


--
Remy Lebeau (TeamB)
Colin Maharaj

Posts: 122
Registered: 12/2/99
Re: Exception reaching catch all, but I don't want it to
Click to report abuse...   Click to reply to this thread Reply
  Posted: Oct 28, 2016 1:44 PM   in response to: Remy Lebeau (Te... in response to: Remy Lebeau (Te...
Thanks, I will look into these further, much to do...

On 24/10/2016 03:14 PM, Remy Lebeau (TeamB) wrote:

Colin wrote:

bool r = TCPClient2->Socket->Readable(5000);

You don't really need to call Readable() directly. If you use the ReadTimeout
property instead, or even the optional ATimeout parameter of ReadLn(), you
can just call ReadLn() by itself and let it raise an EIdReadTimeout exception
if it times out.

catch (EIdConnectException & E)

EIdConnectException is one of several exceptions that TIdTCPClient::Connect()
may throw. But you are not calling Connect() inside your try block, so why
are you expecting EIdConnectException to be catchable?

catch (EIdSocketError & E)
catch (EIdException & E)

Those are two possible exceptions that can be thrown in your try block.
There are others...

if (E.Message == "Connection Closed Gracefully.")

That error message belongs to the EIdConnClosedGracefully exception. So
either write a separate catch() block for EIdConnClosedGracefully directly,
or else yoou can use dynamic_cast to check whether or not your caught EIdException
is actually an EIdConnClosedGracefully.

TCPClient1->IOHandler->CloseGracefully();

You should not be calling CloseGracefully() manually. All that does is sets
the IOHandler's ClosedGracefully property to true, which has already been
done before the EIdConnClosedGracefully exception is thrown. If you are
actually trying to close the socket, you should be calling Disconnect() instead.
Which you should call anyway if you get any socket-related exception,
since you don't know the state of the socket anymore.

catch (...)

You are missing a catch() block for non-Indy VCL Exception types.

Try something more like this insted:

System::String A;
try
{
    TCPClient1->IOHandler->WriteLn("SEND");
    strcpy(Buff, "x");
    A = TCPClient2->IOHandler->ReadLn("\n", 5000);
    // other things with the data....
}
/*
catch (const EIdConnectException & E)
{
    A = E.Message;
}
*/
catch (const EIdSocketError & E)
{
    A = E.Message;
    int e = E.LastError;
}
catch (const EIdConnClosedGracefully &E)
{
    A = E.Message;
    TCPClient1->Disconnect();
}
catch (const EIdException & E)
{
    A = E.Message;
}
catch (const Exception &E)
{
    A = E.Message;
    TCPClient1->Disconnect();
    Sleep(100);
}
catch (...)
{
    A = "Unknown exception...";
    TCPClient1->Disconnect();
    Sleep(100);
}


Alternatively:

System::String A;
try
{
    TCPClient1->IOHandler->WriteLn("SEND");
    strcpy(Buff, "x");
    A = TCPClient2->IOHandler->ReadLn("\n", 5000);
    // other things with the data....
}
catch (const Exception &E)
{
    A = E.Message;
    TCPClient1->Disconnect();
 
    const EIdSocketError *se = dynamic_cast<const EIdSocketError*>(&E);
    if (se)
    {
        int e = se->LastError;
    }
 
    Sleep(100);
}
catch (...)
{
    A = "Unknown exception...";
    TCPClient1->Disconnect();
    Sleep(100);
}

Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02