CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 6 of 6
  1. #1
    Join Date
    May 2003
    Posts
    5

    Calling all Exceptions Guru's

    I'm new to exceptions and I find myself struggling a little. Can some tell me why this doesn't work?


    FILE* hFile;

    try
    {
    // Assume hFile has not been initialised on purpose to generate exception

    //This should raised an exception and jump to the catch block.
    fputs("123", hFile);
    ......
    }
    catch( ... )
    {
    Do_something();
    }


    As I understand it, catch( ...) should catch all exceptions but in this case it doesn't. Can anyone help?

    Spencer

  2. #2
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Calling all Exceptions Guru's

    Originally posted by gingerspen
    I'm new to exceptions and I find myself struggling a little. Can some tell me why this doesn't work?
    The 'C' FILE* streams do not throw exceptions. Unless the underlying fputs() code does a divide by 0, an illegal kernel memory access, or something else that would trigger a system crash, an exception will never be thrown.

    Regards,

    Paul McKenzie

  3. #3
    Join Date
    May 2000
    Location
    KY, USA
    Posts
    18,652
    In addition to Paul's answer...the exception mechanism was introduced with C++ therefore it will not work with ANSI C standard functions...

  4. #4
    Join Date
    May 2003
    Posts
    5

    Re: Re: Calling all Exceptions Guru's

    Originally posted by Paul McKenzie
    The 'C' FILE* streams do not throw exceptions. Unless the underlying fputs() code does a divide by 0, an illegal kernel memory access, or something else that would trigger a system crash, an exception will never be thrown.

    Regards,

    Paul McKenzie

    Paul, thank you very much for your help.

    Spencer

  5. #5
    Join Date
    May 2003
    Posts
    5
    Originally posted by Andreas Masur
    In addition to Paul's answer...the exception mechanism was introduced with C++ therefore it will not work with ANSI C standard functions...
    Andreas, thank you very much for your help.

    Spencer

  6. #6
    Join Date
    Jun 2001
    Location
    Switzerland
    Posts
    4,443
    As a side comment: C++ exceptions are synchronous. There is a second kind of exceptions, structured exceptions (SE), that are asynchronous (and are not part of the C++ standard). An access violation is a SE. Now, Microsoft claims that a C++ catch(...) handler (note the ellipsis) will also catch SEs (VC 6.0). However I saw at least one example where this wasn't true.
    Gabriel, CodeGuru moderator

    Forever trusting who we are
    And nothing else matters
    - Metallica

    Learn about the advantages of std::vector.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured