Exception Handling.
-
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); } //////
-
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); } //////
I left out: #include
-
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); } //////
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
-
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
I tried catch(...), it didnt catch any exceptions either. Something else must be wrong with the code. Thanks, Jerry
-
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
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.