Error control
-
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
-
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
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.
-
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.
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
-
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
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
-
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
Thanks for this advice... Hello World!!! :) from Raphaël