Working BCB4 O/S Exception handler needed

I need a handler for a BCB 4 application ( win 32 ) which will allow me to do the following :-

1 - stop the default operating system exception handler from displaying a dialogue box on the screen.

2 - give my application ( thread ) notification of the exception and pass back an "Abort" to the operating system which is Windows NT workstation 4.

My thread is testing a floppy disk by writing a file to it and I need to detect whether or not a diskette is present and to handle exceptions.
There is NO video MONITOR , this is an indutrial application that uses a serial driven text display device so I don't want to have dialogue boxes displayed that expect a mouse-click to continue program execution.
Points will only be awarded for working code - NO referrals please !
Thanks - Roger
PS. I have tried using the Borland Help examples but they don't work for me.
LVL 2
alcindorAsked:
Who is Participating?
 
chensuConnect With a Mentor Commented:
fopen does not throw an exception. You need to check the return value of fopen. In order to find out the error cause, you should use the Win32 file functions instead, such as CreateFile, and call GetLastError to get the error information. Try the following code.

__try     // Use __try instead of try!!!
{  // we get here as expected
    FILE *fp = fopen("a:\\test","w");   // NO exception
    // check the value of fp

    char *p = NULL;
    *p = 'a';    // EXCEPTION_ACCESS_VIOLATION
}
// the following never gets invoked ?
__except(filter_func(GetExceptionInformation()))
{
}


int filter_func(EXCEPTION_POINTERS *ep)
{ // should be able to determine exception cause here ?
    switch (ep->ExceptionRecord.ExceptionCode)
    {
        case EXCEPTION_ACCESS_VIOLATION:
            // ...
            break;
        // ...
    }

    return  1; // this never gets invoked either ?
}
0
 
chensuCommented:
SetErrorMode
0
 
basantCommented:
You can use Structure Exception handling for that (SEH)
0
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
MadshiCommented:
chensu is right.

This is what I'm calling in the initialiazion of my IO tools unit:

  SetErrorMode(SEM_FAILCRITICALERRORS or SEM_NOOPENFILEERRORBOX);

Regards, Madshi.
0
 
alcindorAuthor Commented:
This only answers the first part of the question.
I need to catch the exception so that I can determine what the failure was ( eg , no diskette in the drive, diskette write protected, write or read failure etc .. ? )

The following is my code (BCB4) for a thread that does the testing - or rather will do when I can sort it out :-

#include <excpt.h>
#pragma package(smart_init)

int filter_func(EXCEPTION_POINTERS *);
EXCEPTION_POINTERS *xp;
extern bool testrequested;

__fastcall test::test(bool CreateSuspended)
        : TThread(CreateSuspended)
{
        SetErrorMode(SEM_FAILCRITICALERRORS);
        FreeOnTerminate = true;
}
//---------------------------------------------------------------------------
void __fastcall test::Execute()
{
   for(;Terminated==false;)
   {
         if(testrequested)
         {
             testrequested = false; // one-shot
             try
             {  // we get here as expected
             fp = fopen("a:\\test","w");
             }
             // the following never gets invoked ?
             __except(filter_func(xp = GetExceptionInformation()))
             {
             }
         }
   }
}
//---------------------------------------------------------------------------
int filter_func(EXCEPTION_POINTERS *)
{ // should be able to determine exception cause here ?
 return  1; // this never gets invoked either ?
}
0
 
alcindorAuthor Commented:
Thanks for your help, can you explain the significance of __try as opposed to try ?

Keep up the good work - Roger
0
 
basantCommented:
__try is OS dependent exception handling which catches OS exception too which are not necessarily a C++ exception. These codes are usually not portable. This is called Structure Exception Handling (SEH).

I tried to answer ur question first. Though I didn't explain it clearly.
0
 
chensuCommented:
try/catch is used for standard C++ exception handling while __try/__except is used for the Structured Exception Handling (SEH) in Windows.
0
All Courses

From novice to tech pro — start learning today.