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
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Exception Handling.

Exception Handling.

Scheduled Pinned Locked Moved C / C++ / MFC
tutorialcomperformancehelpquestion
5 Posts 3 Posters 0 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.
  • J Offline
    J Offline
    jerry1211a
    wrote on last edited by
    #1

    Hey there. Can someone provide me with a few examples of how to handle exceptions? Below is some code I used in an application. p->ShowWindow(SW_SHOW) is used to display a dialog containing an ActiveX Control. If the control is not registered I should get an exception, but it is not working. I must have something set up wrong. I have many places in my application where I need to do exception handling, thought I should start with this example. :) Jerry /////Not sure if I need these 2 lines of code??? #include using std::bad_alloc; /////// try { p->ShowWindow(SW_SHOW); } catch(bad_alloc exception) { MessageBox("Data", "Memory Allocation Error", _OK); } //////

    J S 2 Replies Last reply
    0
    • J jerry1211a

      Hey there. Can someone provide me with a few examples of how to handle exceptions? Below is some code I used in an application. p->ShowWindow(SW_SHOW) is used to display a dialog containing an ActiveX Control. If the control is not registered I should get an exception, but it is not working. I must have something set up wrong. I have many places in my application where I need to do exception handling, thought I should start with this example. :) Jerry /////Not sure if I need these 2 lines of code??? #include using std::bad_alloc; /////// try { p->ShowWindow(SW_SHOW); } catch(bad_alloc exception) { MessageBox("Data", "Memory Allocation Error", _OK); } //////

      J Offline
      J Offline
      jerry1211a
      wrote on last edited by
      #2

      I left out: #include

      1 Reply Last reply
      0
      • J jerry1211a

        Hey there. Can someone provide me with a few examples of how to handle exceptions? Below is some code I used in an application. p->ShowWindow(SW_SHOW) is used to display a dialog containing an ActiveX Control. If the control is not registered I should get an exception, but it is not working. I must have something set up wrong. I have many places in my application where I need to do exception handling, thought I should start with this example. :) Jerry /////Not sure if I need these 2 lines of code??? #include using std::bad_alloc; /////// try { p->ShowWindow(SW_SHOW); } catch(bad_alloc exception) { MessageBox("Data", "Memory Allocation Error", _OK); } //////

        S Offline
        S Offline
        S Senthil Kumar
        wrote on last edited by
        #3

        First off, you really should be catching exceptions by reference, as catching them by value will create two copies, instead of one. That apart, IIRC, bad_alloc is thrown only when the new operator fails to allocate memory. AFAIK, the CRT that comes with VC++ doesn't do that, it instead returns NULL. Your code is most probably throwing some other exception. If you *really* want to catch all exceptions, your code should look like

        try
        {
        ...
        }
        catch(...)
        {
        // Some exception occurred.
        }

        This is not recommended though, I'd suggest figuring out the exact exception thrown and catching it. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

        J N 2 Replies Last reply
        0
        • S S Senthil Kumar

          First off, you really should be catching exceptions by reference, as catching them by value will create two copies, instead of one. That apart, IIRC, bad_alloc is thrown only when the new operator fails to allocate memory. AFAIK, the CRT that comes with VC++ doesn't do that, it instead returns NULL. Your code is most probably throwing some other exception. If you *really* want to catch all exceptions, your code should look like

          try
          {
          ...
          }
          catch(...)
          {
          // Some exception occurred.
          }

          This is not recommended though, I'd suggest figuring out the exact exception thrown and catching it. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

          J Offline
          J Offline
          jerry1211a
          wrote on last edited by
          #4

          I tried catch(...), it didnt catch any exceptions either. Something else must be wrong with the code. Thanks, Jerry

          1 Reply Last reply
          0
          • S S Senthil Kumar

            First off, you really should be catching exceptions by reference, as catching them by value will create two copies, instead of one. That apart, IIRC, bad_alloc is thrown only when the new operator fails to allocate memory. AFAIK, the CRT that comes with VC++ doesn't do that, it instead returns NULL. Your code is most probably throwing some other exception. If you *really* want to catch all exceptions, your code should look like

            try
            {
            ...
            }
            catch(...)
            {
            // Some exception occurred.
            }

            This is not recommended though, I'd suggest figuring out the exact exception thrown and catching it. Regards Senthil _____________________________ My Blog | My Articles | WinMacro

            N Offline
            N Offline
            Nemanja Trifunovic
            wrote on last edited by
            #5

            S. Senthil Kumar wrote: bad_alloc is thrown only when the new operator fails to allocate memory. AFAIK, the CRT that comes with VC++ doesn't do that, it instead returns NULL. It depends on compiler settings.


            My programming blahblahblah blog. If you ever find anything useful here, please let me know to remove it.

            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