Board index » cppbuilder » TFileStream Exceptions - Drive Not Ready

TFileStream Exceptions - Drive Not Ready

I am relatively new to C++Builder and am trying to figure out how to perform
exception handling.  There are a great deal of errors/exceptions that can
occur when working with files and I want to be able to appropriately handle
them.  I have used try and catch to intercept and handle TFileStream
exceptions such as EFOpenError and EFCreateError, but I can't get any
detailed information as to why the error specifically occurred.  For
instance, I want to be able to determine if an EFOpenError is due to no disk
in the A drive and give the user a chance to insert one and retry.  How do I
do this?  In Turbo Pascal and Delphi I could get specific error codes.
Where do I get these specific error codes in C++Builder?  Thanks.

Russ

 

Re:TFileStream Exceptions - Drive Not Ready


Hi Russ,
<snip>
Quote
> I want to be able to determine if an EFOpenError is due to no disk
> in the A drive and give the user a chance to insert one and retry.  
> How do I do this?  In Turbo Pascal and Delphi I could get specific
> error codes.  Where do I get these specific error codes in
> C++Builder?

Use the GetLastError() API function in conjunction with the FileExists()
VCL function.  If the latter returns false, call the former to retireve
the error code.  For example...

#include <memory>

    const char filename[] = "A:\\NoFiles.TXT";

    std::auto_ptr<TFileStream> fs;
    if (FileExists(filename))
    {
        fs.reset(new TFileStream(filename, fmOpenRead));
        //
        //  your code...
        //
    }
    else
    {
        DWORD dwError = GetLastError();
        ShowMessage(SysErrorMessage(dwError));

        switch (dwError)
        {
            case ERROR_NOT_READY:
            {
                // ...
                break;
            }
            case ERROR_FILE_NOT_FOUND:
            {
                // ...
                break;
            }
            case ERROR_PATH_NOT_FOUND:
            {
                // ...
                break;
            }
        }
    }

You can find a complete list of error constants in the <winerror.h>
header file.

Good luck!

--
Damon Chandler (TeamB)
http://bcbcaq.freeservers.com

Re:TFileStream Exceptions - Drive Not Ready


Hey Damon,
    Thanks for the excellent and timely assistance.  Much appreciated.  Keep
up the great work!

Russ

"Damon Chandler (TeamB)" <dm...@cornell.edu> wrote in message
news:38E8F835.B6E14D6C@cornell.edu...

Quote
> Hi Russ,
> <snip>
> > I want to be able to determine if an EFOpenError is due to no disk
> > in the A drive and give the user a chance to insert one and retry.
> > How do I do this?  In Turbo Pascal and Delphi I could get specific
> > error codes.  Where do I get these specific error codes in
> > C++Builder?

> Use the GetLastError() API function in conjunction with the FileExists()
> VCL function.  If the latter returns false, call the former to retireve
> the error code.  For example...

> #include <memory>

>     const char filename[] = "A:\\NoFiles.TXT";

>     std::auto_ptr<TFileStream> fs;
>     if (FileExists(filename))
>     {
>         fs.reset(new TFileStream(filename, fmOpenRead));
>         //
>         //  your code...
>         //
>     }
>     else
>     {
>         DWORD dwError = GetLastError();
>         ShowMessage(SysErrorMessage(dwError));

>         switch (dwError)
>         {
>             case ERROR_NOT_READY:
>             {
>                 // ...
>                 break;
>             }
>             case ERROR_FILE_NOT_FOUND:
>             {
>                 // ...
>                 break;
>             }
>             case ERROR_PATH_NOT_FOUND:
>             {
>                 // ...
>                 break;
>             }
>         }
>     }

> You can find a complete list of error constants in the <winerror.h>
> header file.

> Good luck!

> --
> Damon Chandler (TeamB)
> http://bcbcaq.freeservers.com

Other Threads