Exceptions: Catching null memory?
-
Given this: try { MyClass->DoSomething(); } catch (...) { cout << "Unknown exception"; } I am trying to catch a null memory exception (where MyClass is NULL). Is there a specific way to catch this, or am I conformed to using the catch all "..." method, or sticking with "if (MyClass!=NULL)"?
-
Given this: try { MyClass->DoSomething(); } catch (...) { cout << "Unknown exception"; } I am trying to catch a null memory exception (where MyClass is NULL). Is there a specific way to catch this, or am I conformed to using the catch all "..." method, or sticking with "if (MyClass!=NULL)"?
You can use this:
catch (PostingCPlusPlusQuestionsInLoungeException e)
{
cout << "Please use the Visual C++ forum." << endl;
}Regards, Alvaro
-
Given this: try { MyClass->DoSomething(); } catch (...) { cout << "Unknown exception"; } I am trying to catch a null memory exception (where MyClass is NULL). Is there a specific way to catch this, or am I conformed to using the catch all "..." method, or sticking with "if (MyClass!=NULL)"?
Please post all programming related questions in their respective forums. Please read the heading on the lounge, it states "The Lounge is a place where you can discuss anything that takes your fancy. If you just want to laze about and discuss things that don't quite fit elsewhere, then this is the place." BTW: I use several API functions for e.g. IsBad****(...) to know the status of objects and functions. HTH :) ;) ;P :-D :cool: Farhan Noor Qureshi
-
You can use this:
catch (PostingCPlusPlusQuestionsInLoungeException e)
{
cout << "Please use the Visual C++ forum." << endl;
}Regards, Alvaro
-
Given this: try { MyClass->DoSomething(); } catch (...) { cout << "Unknown exception"; } I am trying to catch a null memory exception (where MyClass is NULL). Is there a specific way to catch this, or am I conformed to using the catch all "..." method, or sticking with "if (MyClass!=NULL)"?
Yes, it's easy. the point in using _set_se_translator API.
struct se_exception
{
unsigned code;
se_exception (unsigned c) : code(c) {}
};struct se_exception_access_vioalation : se_exception
{
se_exception_access_vioalation ()
: se_exception(EXCEPTION_ACCESS_VIOLATION) {}
};void translator_func(unsigned u, _EXCEPTION_POINTERS *)
{
switch (u)
{
case EXCEPTION_ACCESS_VIOLATION:
throw se_exception_access_vioalation();
break;
default:
throw se_exception(u);
break;
}
}struct dummy
{
char * s;
};int main(int argc, char* argv[])
{
_set_se_translator(translator_func);dummy \* p = 0; try { std::cout << p->s <
:)
-
Yes, it's easy. the point in using _set_se_translator API.
struct se_exception
{
unsigned code;
se_exception (unsigned c) : code(c) {}
};struct se_exception_access_vioalation : se_exception
{
se_exception_access_vioalation ()
: se_exception(EXCEPTION_ACCESS_VIOLATION) {}
};void translator_func(unsigned u, _EXCEPTION_POINTERS *)
{
switch (u)
{
case EXCEPTION_ACCESS_VIOLATION:
throw se_exception_access_vioalation();
break;
default:
throw se_exception(u);
break;
}
}struct dummy
{
char * s;
};int main(int argc, char* argv[])
{
_set_se_translator(translator_func);dummy \* p = 0; try { std::cout << p->s <
:)
I had never seen that before... might come in handy sometime. Thanks. According to the docs, though, you should save the return value from the _set_se_translator() call and restore it when you are done.
...
_se_translator_function old = _set_se_translator(translator_func);
...
_set_se_translator(old);
...J
-
Please post all programming related questions in their respective forums. Please read the heading on the lounge, it states "The Lounge is a place where you can discuss anything that takes your fancy. If you just want to laze about and discuss things that don't quite fit elsewhere, then this is the place." BTW: I use several API functions for e.g. IsBad****(...) to know the status of objects and functions. HTH :) ;) ;P :-D :cool: Farhan Noor Qureshi
Farhan Noor Qureshi wrote: Please post all programming related questions in their respective forums. Please read the heading on the lounge, it states "The Lounge is a place where you can discuss anything that takes your fancy. If you just want to laze about and discuss things that don't quite fit elsewhere, then this is the place." Isn't this a contradiction? If "The Lounge is a place where you can discuss anything..., doesn't the set of all subjects contain the set of subjects dealing with coding? Just being pedantic. ;-) Tim Lesher http://www.lesher.ws