CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Catching OS exceptions

    Is there a way to catch exceptions that get generated by the OS? For example...

    Code:
    try {
    	fread(NULL, 2, 3, NULL); // This produces an Access Violation error
    } catch (...) {
    	int x = 3; // But we don't seem to reach this line
    }
    In older versions of VC++ I've a recollection that they offered capitalized versions (i.e. TRY/CATCH) but I'm not too sure if they still exist any more...
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Catching OS exceptions

    I think you're referring to Microsoft SEH - which uses _try _except _finally. The options are set under Code Generation

    See
    https://docs.microsoft.com/en-us/cpp...?view=msvc-160
    https://docs.microsoft.com/en-us/cpp...&view=msvc-160
    https://social.msdn.microsoft.com/Fo...tion-exception
    Last edited by 2kaud; March 29th, 2021 at 07:49 AM.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  3. #3
    John E is offline Elite Member Power Poster
    Join Date
    Apr 2001
    Location
    Manchester, England
    Posts
    4,835

    Re: Catching OS exceptions

    Many thanks 2kaud - you're probably right about SEH and __try / __except etc.

    I tried those suggestions but they don't work here in the case of Access Violations. Instead, the VC debugger gives me this dialog:-

    Name:  Capture-05.PNG
Views: 1340
Size:  10.8 KB

    So it looks like certain exceptions just aren't catchable...
    "A problem well stated is a problem half solved.” - Charles F. Kettering

  4. #4
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    7,822

    Re: Catching OS exceptions

    You might find more info here https://docs.microsoft.com/en-us/win...ption-handling

    It seems there are various windows functions that can be used for dealing with exceptions (eg SetUnhandledExceptionFilter). I haven't used them so can't advise further on them.
    All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!

    C++23 Compiler: Microsoft VS2022 (17.6.5)

  5. #5
    Join Date
    May 2001
    Location
    Germany
    Posts
    1,158

    Re: Catching OS exceptions

    The second link in 2kaud's post #2 seems to imply that /EHa might be what you are looking for. I don't have experience with this, either.

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