为什么我的程序没有从解除引用空指针中捕获异常?

Jon*_*Jon 4 c++ exception-handling

有人可以解释为什么没有发现这个例外.

try {
    // This will cause an exception
    char *p = 0;
    char x = 0;
    *p = x;
}
catch (...) {
    int a = 0;
}
Run Code Online (Sandbox Code Playgroud)

当我运行程序时它会在*p = x行上死掉.我希望catch块会导致忽略此异常.

我正在使用Qt Creator(2.2)和Qt 4.7.2在Windows 7 32位上使用Visual Studios 2008进行编译.

CB *_*ley 10

这里没有抛出C++异常.您正在导致未定义的行为,因为*p = xderefences指针具有空指针值.

仅当您或您调用的代码执行throw表达式时,才会传播异常.通常不能使用未定义的行为catch.