Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
CODE PROJECT For Those Who Code
  • Home
  • Articles
  • FAQ
Community
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Structured exception vs C++ exception

Structured exception vs C++ exception

Scheduled Pinned Locked Moved C / C++ / MFC
c++visual-studiotutorialquestion
6 Posts 3 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L Offline
    L Offline
    Lane Yu
    wrote on last edited by
    #1

    How to rasie a exception that can only be caught by __try/__except, but cannot be caught by try/catch? In a Windows application written by VC. What time to use structured exception instead of C++ exception?

    S 1 Reply Last reply
    0
    • L Lane Yu

      How to rasie a exception that can only be caught by __try/__except, but cannot be caught by try/catch? In a Windows application written by VC. What time to use structured exception instead of C++ exception?

      S Offline
      S Offline
      Stephen Hewitt
      wrote on last edited by
      #2

      In standard C++ try/catch can only catch C++ exceptions: that is only exceptions that are raised by the C++ throw statement. Unfortunately some version of Microsoft Visual C++ (6.0 and below) do not follow the standard in this regard. This was fixed in later versions but the old non-standard behaviour can be selected with the /EHa[^] switch (not a good idea in general). __try/__except is a low level construct and can catch any exception. Note however that with __try/__except the destructors of objects on the stack as it is unwound are not called, so this construct doesn’t play nice with C++. It will break the RAII pattern for example. To explicitly raise a C++ exception use the C++ throw keyword. To explicitly raise an exception (not a C++ exception) you have two options:    1. Do something wrong! For example to raise a divide by zero exception try to divide by zero; to raise an access violation attempt to access an invalid memory location.    2. Use the Win32 RaiseException[^] function.

      Steve

      M L 2 Replies Last reply
      0
      • S Stephen Hewitt

        In standard C++ try/catch can only catch C++ exceptions: that is only exceptions that are raised by the C++ throw statement. Unfortunately some version of Microsoft Visual C++ (6.0 and below) do not follow the standard in this regard. This was fixed in later versions but the old non-standard behaviour can be selected with the /EHa[^] switch (not a good idea in general). __try/__except is a low level construct and can catch any exception. Note however that with __try/__except the destructors of objects on the stack as it is unwound are not called, so this construct doesn’t play nice with C++. It will break the RAII pattern for example. To explicitly raise a C++ exception use the C++ throw keyword. To explicitly raise an exception (not a C++ exception) you have two options:    1. Do something wrong! For example to raise a divide by zero exception try to divide by zero; to raise an access violation attempt to access an invalid memory location.    2. Use the Win32 RaiseException[^] function.

        Steve

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        Stephen Hewitt wrote:

        so this construct doesn’t play nice with C++.

        Excellent reply. Where were you when I learned that the hard way? :)

        "Posting a VB.NET question in the C++ forum will end in tears." Chris Maunder

        1 Reply Last reply
        0
        • S Stephen Hewitt

          In standard C++ try/catch can only catch C++ exceptions: that is only exceptions that are raised by the C++ throw statement. Unfortunately some version of Microsoft Visual C++ (6.0 and below) do not follow the standard in this regard. This was fixed in later versions but the old non-standard behaviour can be selected with the /EHa[^] switch (not a good idea in general). __try/__except is a low level construct and can catch any exception. Note however that with __try/__except the destructors of objects on the stack as it is unwound are not called, so this construct doesn’t play nice with C++. It will break the RAII pattern for example. To explicitly raise a C++ exception use the C++ throw keyword. To explicitly raise an exception (not a C++ exception) you have two options:    1. Do something wrong! For example to raise a divide by zero exception try to divide by zero; to raise an access violation attempt to access an invalid memory location.    2. Use the Win32 RaiseException[^] function.

          Steve

          L Offline
          L Offline
          Lane Yu
          wrote on last edited by
          #4

          But in my testing program, all kinds of exceptions including zero-dividing or NULL pointer can be caught by catch(...). Why? Does that mean we can always use C++ try/catch in C++ programs?

          S 1 Reply Last reply
          0
          • L Lane Yu

            But in my testing program, all kinds of exceptions including zero-dividing or NULL pointer can be caught by catch(...). Why? Does that mean we can always use C++ try/catch in C++ programs?

            S Offline
            S Offline
            Stephen Hewitt
            wrote on last edited by
            #5

            Lane Yu wrote:

            But in my testing program, all kinds of exceptions including zero-dividing or NULL pointer can be caught by catch(...). Why? Does that mean we can always use C++ try/catch in C++ programs?

            As my previous post indicated, the fact that you can use try/catch to catch NULL pointer references and zero-dividing indicates that:  - You're using an old and non-standard compiler such as Microsoft Visual Studio 6.  - You're using a compiler with a non-standard extension with enables this non-standard behaviour; such as the later Microsoft compiler's /EHa switch. In standard C++ try/catch can NOT catch these exceptions!

            Steve

            L 1 Reply Last reply
            0
            • S Stephen Hewitt

              Lane Yu wrote:

              But in my testing program, all kinds of exceptions including zero-dividing or NULL pointer can be caught by catch(...). Why? Does that mean we can always use C++ try/catch in C++ programs?

              As my previous post indicated, the fact that you can use try/catch to catch NULL pointer references and zero-dividing indicates that:  - You're using an old and non-standard compiler such as Microsoft Visual Studio 6.  - You're using a compiler with a non-standard extension with enables this non-standard behaviour; such as the later Microsoft compiler's /EHa switch. In standard C++ try/catch can NOT catch these exceptions!

              Steve

              L Offline
              L Offline
              Lane Yu
              wrote on last edited by
              #6

              Thank you. I'm using VC6.

              1 Reply Last reply
              0
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Categories
              • Recent
              • Tags
              • Popular
              • World
              • Users
              • Groups