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. Error control

Error control

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
5 Posts 2 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.
  • R Offline
    R Offline
    Raphael Kindt
    wrote on last edited by
    #1

    Hello world... Anybody have a good idea to control the errors in a program? I often return an error ID from a function... I've already use Exception but I found that not very usefull... I've tried with a specific thread which uses a loop. Like this:

    volatile int error = 0;  // global
    CEvent g_eventStart;  // global
    
    UINT CatchErrorThread(LPVOID pParam)
    {
       CWnd* MyDlg = CWnd::FromHandle((HWND)pParam);
       do {
          ::WaitForSingleObject(g_eventStart, INFINITE);
          g_eventStart.ResetEvent();
          switch(error) {
          case 0:
             break;
          case 1:
             MyDlg->MessageBox("error message 1", "catch error", MB_ICONERROR | MB_OK);
             break;
          case 2:
             MyDlg->MessageBox("error message 2", "catch error", MB_ICONERROR | MB_OK);
             break;
          default:
             MyDlg->MessageBox("unknown message", "catch error", MB_ICONERROR | MB_OK);
             break;
          }
       } while(error != 0);
       return 0;
    }
    

    There is not another solution to make pass the errors??? Anybody have another solution??? Thanks in advance... Hello World!!! :) from Raphaël

    B 1 Reply Last reply
    0
    • R Raphael Kindt

      Hello world... Anybody have a good idea to control the errors in a program? I often return an error ID from a function... I've already use Exception but I found that not very usefull... I've tried with a specific thread which uses a loop. Like this:

      volatile int error = 0;  // global
      CEvent g_eventStart;  // global
      
      UINT CatchErrorThread(LPVOID pParam)
      {
         CWnd* MyDlg = CWnd::FromHandle((HWND)pParam);
         do {
            ::WaitForSingleObject(g_eventStart, INFINITE);
            g_eventStart.ResetEvent();
            switch(error) {
            case 0:
               break;
            case 1:
               MyDlg->MessageBox("error message 1", "catch error", MB_ICONERROR | MB_OK);
               break;
            case 2:
               MyDlg->MessageBox("error message 2", "catch error", MB_ICONERROR | MB_OK);
               break;
            default:
               MyDlg->MessageBox("unknown message", "catch error", MB_ICONERROR | MB_OK);
               break;
            }
         } while(error != 0);
         return 0;
      }
      

      There is not another solution to make pass the errors??? Anybody have another solution??? Thanks in advance... Hello World!!! :) from Raphaël

      B Offline
      B Offline
      Bill Wilson
      wrote on last edited by
      #2

      Pretty hard to tell what this code is supposed to accomplish. Is the variable error assigned somewhere? It appears to be undefined in your code. Maybe you need something like int error = g_eventStart.ResetEvent(); although that is just a guess. The code you presented would, of course, not even compile, unless error is some kind of global, set by the ResetEvent method?

      **You wrote:

      I've already use Exception but I found that not very usefull...
      I've tried with a specific thread which uses a loop. Like this:**

      This makes it sound like you are trying to use a thread in a loop to perform the same function as exception handling. Sorry, but that makes absolutely no sense. Handling errors is one topic, but I fail to see what a separate thread has to do with anything. I think you should try harder with exceptions.

      R 1 Reply Last reply
      0
      • B Bill Wilson

        Pretty hard to tell what this code is supposed to accomplish. Is the variable error assigned somewhere? It appears to be undefined in your code. Maybe you need something like int error = g_eventStart.ResetEvent(); although that is just a guess. The code you presented would, of course, not even compile, unless error is some kind of global, set by the ResetEvent method?

        **You wrote:

        I've already use Exception but I found that not very usefull...
        I've tried with a specific thread which uses a loop. Like this:**

        This makes it sound like you are trying to use a thread in a loop to perform the same function as exception handling. Sorry, but that makes absolutely no sense. Handling errors is one topic, but I fail to see what a separate thread has to do with anything. I think you should try harder with exceptions.

        R Offline
        R Offline
        Raphael Kindt
        wrote on last edited by
        #3

        My aim is to catch an error ID and display a message error corresponding to this ID. I try to find any methods who can help me... My code before work correctly... the variable error is global. When a error occure (for example a NULL handle is an error), I do this :

        if(!MyHandle) {
        error = ERR_001; // with #define ERR_001 1
        g_eventStart.SetEvent(); // I display the error message with this ID
        }

        The advantage is that I can do that in my program anywhere. But, yes, I'm a not an expert with Exception... Thus this thread with his loop is doing the same thing as Exception! I didn't know that! What methods use you to catch error? thanks Hello World!!! :) from Raphaël

        B 1 Reply Last reply
        0
        • R Raphael Kindt

          My aim is to catch an error ID and display a message error corresponding to this ID. I try to find any methods who can help me... My code before work correctly... the variable error is global. When a error occure (for example a NULL handle is an error), I do this :

          if(!MyHandle) {
          error = ERR_001; // with #define ERR_001 1
          g_eventStart.SetEvent(); // I display the error message with this ID
          }

          The advantage is that I can do that in my program anywhere. But, yes, I'm a not an expert with Exception... Thus this thread with his loop is doing the same thing as Exception! I didn't know that! What methods use you to catch error? thanks Hello World!!! :) from Raphaël

          B Offline
          B Offline
          Bill Wilson
          wrote on last edited by
          #4

          You could do something like this... in the calling program

          try {
          int x = SomeProcedure();
          ...

          }
          catch (CMyException *e)
          {
          // Do something with the error return
          LogError(e->nMyErrorNum....

           delete e;
          

          }

          int SomeProcedure()
          {
          n = new CMyClass();
          if (!n) {
          CMyException* pErr = new CMyException(TRUE);
          pErr->errNum = ERRORNUMBERIWANTTOUSE;
          throw pErr;
          }
          ...

          Define class CMyException

          class CMyException : public CException {
          int nMyErrorNum;

          ...

          Hope this helps

          R 1 Reply Last reply
          0
          • B Bill Wilson

            You could do something like this... in the calling program

            try {
            int x = SomeProcedure();
            ...

            }
            catch (CMyException *e)
            {
            // Do something with the error return
            LogError(e->nMyErrorNum....

             delete e;
            

            }

            int SomeProcedure()
            {
            n = new CMyClass();
            if (!n) {
            CMyException* pErr = new CMyException(TRUE);
            pErr->errNum = ERRORNUMBERIWANTTOUSE;
            throw pErr;
            }
            ...

            Define class CMyException

            class CMyException : public CException {
            int nMyErrorNum;

            ...

            Hope this helps

            R Offline
            R Offline
            Raphael Kindt
            wrote on last edited by
            #5

            Thanks for this advice... Hello World!!! :) from Raphaël

            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