• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

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.
0
alcindor
Asked:
alcindor
  • 4
  • 2
1 Solution
 
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
 
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Featured Post

2018 Annual Membership Survey

Here at Experts Exchange, we strive to give members the best experience. Help us improve the site by taking this survey today! (Bonus: Be entered to win a great tech prize for participating!)

  • 4
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now