空指针的异常 [英] exceptions with null pointers

查看:140
本文介绍了空指针的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,

在以下代码中我有段故障而不是异常。

为什么?在这种情况下我必须做些什么来捕捉异常?使用

编译器:gcc版本3.3.6(Debian 1:3.3.6-13)


int main()

{

尝试{

int * p = NULL;

* p = 4;

}

catch(...){

cout<< "例外" <<结束;

}

返回0;

}


谢谢,

Denis。

Hello,
in the following code i have segmentaion fault instead of exception.
Why? What i must to do to catch exceptions in such situation? Used
compiler: gcc version 3.3.6 (Debian 1:3.3.6-13)

int main()
{
try{
int* p = NULL;
*p = 4;
}
catch(...){
cout << "exception" << endl;
}
return 0;
}

Thanks,
Denis.

推荐答案

Denis Petronenko写道:
Denis Petronenko wrote:

在下面的代码中,我有段故障而不是异常。

为什么?在这种情况下我必须做些什么来捕捉异常?使用

编译器:gcc版本3.3.6(Debian 1:3.3.6-13)


int main()

{

尝试{

int * p = NULL;

* p = 4;

}

catch(...){

cout<< "例外" <<结束;

}

返回0;

}
in the following code i have segmentaion fault instead of exception.
Why? What i must to do to catch exceptions in such situation? Used
compiler: gcc version 3.3.6 (Debian 1:3.3.6-13)

int main()
{
try{
int* p = NULL;
*p = 4;
}
catch(...){
cout << "exception" << endl;
}
return 0;
}



取消引用null指针产生*未定义的行为*。它可以(允许)抛出异常,或者它可能会向你的主管发送一封讨厌的电子邮件

,然后重新格式化你的硬盘。期待

来自具有不确定行为的构造*确定*

很傻。


你什么都做不了肯定会遇到异常。即使你对你的系统/编译器有所了解,它会引导你一个解决方案,但是它不能在不同的系统或编译器上运行,并且<因此,
不能归类为C ++解决方案。一个简单的答案

就是这样,在取消引用之前检查你的指针是否为空。


V

-

请在通过电子邮件回复时删除资金''A'

我没有回复最热门的回复,请不要问

Dereferencing a null pointer produces *undefined behaviour*. It
may (allowed to) throw an exception, or it may send a nasty e-mail
to your supervisor and then reformat your hard drive. Expecting
something *definite* from a construct that has undefined behaviour
is silly.

You can''t do anything certain to catch an exception. Even if you
figure something out about your system/compiler which will lead you
to a solution, it won''t work on a different system or compiler, and
therefore cannot be classified as a C++ solution. A simple answer
to this is, "check your pointer for being null before dereferencing".

V
--
Please remove capital ''A''s when replying by e-mail
I do not respond to top-posted replies, please don''t ask


Denis Petronenko< pe ******** @ gmail.comwrote:
Denis Petronenko <pe********@gmail.comwrote:

,代码如下我有段故障而不是异常。
in the following code i have segmentaion fault instead of exception.



语言中没有任何内容表示解除引用空指针

将对异常进行。如果您需要使用此类

保修的语言,请切换到C#或Java。在这种情况下C ++(和C)

实际发生的事情是未定义的行为,这意味着程序可以自由地执行任何操作(包括向你的老板发送侮辱)。 br />

there is nothing in the language that says "dereferencing null pointer
will invode exception". If you need language with this sort of
warranties, switch to C# or Java. What actually happens in C++ (and C)
in such situation is undefined behaviour, which means program is free to
do anything (sending insults to your boss included).


为什么?在这种情况下我必须做些什么来捕捉异常?
Why? What i must to do to catch exceptions in such situation?



您无法做任何事情并保持您的程序可移植。如果您想要特定于您的操作系统的

答案,请再次询问另一组。解除引用

空指针是编程错误,修复编程错误

(而不是隐藏它们)通常是解决不稳定程序的最佳解决方案。

B.

There is nothing you can do and keep your program portable. If you want
answer specific to your OS, ask again on another group. Dereferencing
null pointer is programming error, and fixing programming errors
(instead of hiding them) is usually best solution to erratic program
behaviour.
B.


Denis Petronenko写道:
Denis Petronenko wrote:

in以下代码我有段故障而不是异常。

为什么?在这种情况下我必须做些什么来捕捉异常?使用

编译器:gcc版本3.3.6(Debian 1:3.3.6-13)


int main()

{

尝试{

int * p = NULL;

* p = 4;

}

catch(...){

cout<< "例外" <<结束;

}

返回0;

}
in the following code i have segmentaion fault instead of exception.
Why? What i must to do to catch exceptions in such situation? Used
compiler: gcc version 3.3.6 (Debian 1:3.3.6-13)

int main()
{
try{
int* p = NULL;
*p = 4;
}
catch(...){
cout << "exception" << endl;
}
return 0;
}



取消引用null指针不会抛出异常;它导致

未定义的行为,这可能是崩溃,内存损坏,或者是你的银行账户清空 - 这真的是未定义的。一些

编译器有扩展(例如微软的结构化异常处理和b $ b处理),可以将这些错误转换为异常,但是这些

不标准。


如果你想要一个例外,请这样做:


struct MyException:std :: exception

{

虚拟const char * what()const

{return"我的异常发生了'; }

};


int main()

{

试试{

int * p = NULL;

如果(!p)抛出MyException();

* p = 4;

}

catch(const std :: exception& e){

cout<< "异常:" << e.what()<<结束;

}

返回0;

}


干杯! --M

Dereferencing a null pointer does not throw an exception; it causes
undefined behavior, which could be a crash, a corruption of memory, or
the emptying of your bank account -- it''s really undefined. Some
compilers have extensions (such as Microsoft''s "structured exception
handling") that can translate such errors into exceptions, but these
are not standard.

If you want an exception, do this:

struct MyException : std::exception
{
virtual const char* what() const
{ return "My exception happened"; }
};

int main()
{
try{
int* p = NULL;
if( !p ) throw MyException();
*p = 4;
}
catch( const std::exception& e ){
cout << "exception:" << e.what() << endl;
}
return 0;
}

Cheers! --M


这篇关于空指针的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆