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. try catch question

try catch question

Scheduled Pinned Locked Moved C / C++ / MFC
comperformancehelptutorialquestion
5 Posts 4 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.
  • D Offline
    D Offline
    Dennis L
    wrote on last edited by
    #1

    Hi all! Can anyone tell me if i could use try-catch block within a windows procedure to catch errors like "Error occurred while creating window!" or "Couldn't initialize COM object!" for example my in winmain function: try { SetupMessageBoxPointer(); // Initialize WinSock for peer to peer classes, and COM for ... if (!InitializeWinSock2()) throw Error(L"Error: Cannot initialize Ws2_32.dll!", _FATAL_ERROR); if ((hOK = CoInitialize(NULL)) != S_OK) throw Error(L"Error: Cannot initialize COM object!", _FATAL_ERROR); mainapp = new DataCrypt(hInstance); if (mainapp == NULL) throw Error(L"Cannot allocate memory for DataCrypt object!", _FATAL_ERROR); mainapp->Initialize(); mainapp->Run(); // messages loop } catch(Error &err) { ERRORMSG(NULL, err.GetText(), err.GetErrorType()); }

    A M 2 Replies Last reply
    0
    • D Dennis L

      Hi all! Can anyone tell me if i could use try-catch block within a windows procedure to catch errors like "Error occurred while creating window!" or "Couldn't initialize COM object!" for example my in winmain function: try { SetupMessageBoxPointer(); // Initialize WinSock for peer to peer classes, and COM for ... if (!InitializeWinSock2()) throw Error(L"Error: Cannot initialize Ws2_32.dll!", _FATAL_ERROR); if ((hOK = CoInitialize(NULL)) != S_OK) throw Error(L"Error: Cannot initialize COM object!", _FATAL_ERROR); mainapp = new DataCrypt(hInstance); if (mainapp == NULL) throw Error(L"Cannot allocate memory for DataCrypt object!", _FATAL_ERROR); mainapp->Initialize(); mainapp->Run(); // messages loop } catch(Error &err) { ERRORMSG(NULL, err.GetText(), err.GetErrorType()); }

      A Offline
      A Offline
      Atropus
      wrote on last edited by
      #2

      With the code you've posted, catch block surelly handles the Error exceptions you throw; anyway Windows API (without using MFC) doesn't use exception mchanism.

      1 Reply Last reply
      0
      • D Dennis L

        Hi all! Can anyone tell me if i could use try-catch block within a windows procedure to catch errors like "Error occurred while creating window!" or "Couldn't initialize COM object!" for example my in winmain function: try { SetupMessageBoxPointer(); // Initialize WinSock for peer to peer classes, and COM for ... if (!InitializeWinSock2()) throw Error(L"Error: Cannot initialize Ws2_32.dll!", _FATAL_ERROR); if ((hOK = CoInitialize(NULL)) != S_OK) throw Error(L"Error: Cannot initialize COM object!", _FATAL_ERROR); mainapp = new DataCrypt(hInstance); if (mainapp == NULL) throw Error(L"Cannot allocate memory for DataCrypt object!", _FATAL_ERROR); mainapp->Initialize(); mainapp->Run(); // messages loop } catch(Error &err) { ERRORMSG(NULL, err.GetText(), err.GetErrorType()); }

        M Offline
        M Offline
        Matthew Faithfull
        wrote on last edited by
        #3

        Yes, your idea is sound. Clearly you will need to define the Error class appropriately and to enable C++ exception handling for your project. Catching exceptions by reference is also occasionaly flaky so if you find the catch part is not working then trying throwing and catching an Error pointer instead.

        "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

        S 1 Reply Last reply
        0
        • M Matthew Faithfull

          Yes, your idea is sound. Clearly you will need to define the Error class appropriately and to enable C++ exception handling for your project. Catching exceptions by reference is also occasionaly flaky so if you find the catch part is not working then trying throwing and catching an Error pointer instead.

          "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

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

          Matthew Faithfull wrote:

          Catching exceptions by reference is also occasionaly flaky

          I'm not sure what you mean by this. Catching exceptions by reference (usually a const reference) is the norm.

          Steve

          M 1 Reply Last reply
          0
          • S Stephen Hewitt

            Matthew Faithfull wrote:

            Catching exceptions by reference is also occasionaly flaky

            I'm not sure what you mean by this. Catching exceptions by reference (usually a const reference) is the norm.

            Steve

            M Offline
            M Offline
            Matthew Faithfull
            wrote on last edited by
            #5

            I was recently looking into this because I've been implementing exception handling myself, as opposed to just using it, ( based on porting a VC6 OSS implementation). There's a set of matching rules that decide whether a particular catch handler matches a particular thrown exception. The more I looked into how to implement this the more complex I realised it is. Added to which the flags in the underlying Windows (and VC++ generated code ) exception information don't seem adequate to accurately differentiate between for example a reference to a type and a reference to a pointer type. On the basis that I could find no documentation on these rules, maybe they're buried in an appendix of the C++ spec somewhere, I determined to advise extreme caution when throwing anything other than a straight forward pointer. Plain pointers are clearly flagged in the built in data structures and can definitely be caught reliably. I need to do a good deal more investigation, and ideally get hold of the formal rules, to determine if VC++ does indeed generate enough information to fully implement them and whether MSs own exception system has any holes in this area which I strongly suspect it might. I have stuggled in the past to get catch handlers to match with anything other than very simple thrown types and now I think I know why.

            "The secret of happiness is freedom, and the secret of freedom, courage." Thucydides (B.C. 460-400)

            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