Re: Which exception it is?
On 23 Jun, 09:46, "mos" <mmosqu...@163.com> wrote:
Hi!
When call a null function pointer will cause a execption,
Says who?
but it can't
be caught as std::exception, then which exception it is?
The following code is a example:
typedef void (*testfunc)(int a);
void test()
{
testfunc func = NULL;
func(10);
Dereferencing a null pointer.
iTest* p = NULL;
p->Test(10);
Dereferencing a null pointer
}
int main()
{
try
{
test();
}
catch(std::exception& x)
{
std::cout << "exception: " << x.what() << std::endl;
}
catch(...)
{
std::cout << "unknown" << std::endl;
}
return 0;
}
Dereferencing a null pointer has undefined behaviour. Anything can
happen. What you implementation does with this code is outside the
scope of the C++ language. There is certainly no reason to expect a C+
+ exception to be thrown. The only thing that will be caught in a
catch block is a C++ exception generated by a throw statement, and
there isn't a throw statement in your code.
Given the right settings, your implementation *may* offer, *as an
extension to the language* (and so, outside the scope of this
newsgroup), predictable behaviour in the event of dereferencing a null
pointer. Your implementation may use the word "exception" to describe
some aspect of that behaviour. However, that is not the same thing as
a C++ exception.
Whether your implementation provides such an extension, how to use it
if it does, and whether it hooks into the C++ try-throw-catch syntax
in some way are all questions that are off-topic in this group. You
need to look in your compiler documentation and ask about it in a
compiler-specific newsgroup. Some suggestions in the FAQ:
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.9
Gavin Deane