Borland BCB4 Exception handling

I need a working BCB 4 example of how to catch  ( intercept ) an "EAccessViolation" exception as may be generated by reference to a null pointer for example ?
I am getting this error ocassionally and need to find out where it is  occurring an take measures to prevent the program from terminating.
No points given without a good proven example.
LVL 2
Roger AlcindorAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

chensuCommented:
#include <windows.h>
#include <stdio.h>

int main( void );

int main( void )
{
    __try
    {
        char *p = NULL;
        *p = 'a';
    }
    __except (::GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
                EXCEPTION_EXECUTE_HANDLER :
                EXCEPTION_CONTINUE_SEARCH)
    {
        printf("Access Violation Caught!\n");
    }

    return 0;
}
0
chensuCommented:
>need to find out where it is occurring

Why don't you debug it and view the Call Stack when it happens?
0
Roger AlcindorAuthor Commented:
I have tried your code as shown below , note that I'm not using a console application. I have a form with a button on it whose action is to generate an EAccessViolation exception by using a NULL pointer. Your code fragment as applied below makes no difference, setting a break point at your code indicates that it never gets called ?

My code with Borland compiler generated excepton handling commented out :-

#include <vcl.h>
#pragma hdrstop
USERES("Project1.res");
USEFORM("Unit1.cpp", Form1);
//---------------------------------------------------------------------------
WINAPI WinMain(HINSTANCE, HINSTANCE, LPSTR, int)
{
    int x;
    try
    {
         Application->Initialize();
         Application->CreateForm(__classid(TForm1), &Form1);
         Application->Run();
    }

// your code tried out here ...
    __except (::GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION ?
                EXCEPTION_EXECUTE_HANDLER :
                EXCEPTION_CONTINUE_SEARCH)
    {
        ShowMessage("Access Violation Caught!\n");
    }

// original Borland generated code

//    catch (Exception &exception)
//    {
//         Application->ShowException(&exception);
//    }

    return 0;
}
0
Cloud Class® Course: SQL Server Core 2016

This course will introduce you to SQL Server Core 2016, as well as teach you about SSMS, data tools, installation, server configuration, using Management Studio, and writing and executing queries.

chensuCommented:
1. You should not reject a correct answer.

2. The code you are catching the exception from is just the message loop. When the button is pressed, a message is sent and the code at somewhere else which is beyond the __try block is executed. How can it catch the exception?

3. It should be __try instead of try.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Roger AlcindorAuthor Commented:
Is there a reference article that describes the Borland exception
handling features ? Something that explains the difference between try and __try .

Thanks for your help - Roger
0
chensuCommented:
The standard C++ exception handling uses try while the Structured Exception Handling (SEH) uses __try.

Take a look at the following article in the Visual C++ Programmer's Guide. Just ignore the MFC exceptions part.

Exception Handling: Overview
http://msdn.microsoft.com/isapi/msdnlib.idc?theURL=/library/devprods/vs6/visualc/vccore/_core_exception_handling.3a_.overview.htm
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Development

From novice to tech pro — start learning today.