捕捉类异常 [英] Catching class exception

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

问题描述

尝试

{

...

}

catch(例外e)

{

cout<< e.what()<<结束;

}


在上面的代码中,e是按值而不是多态地捕获的(假设

表示抛出了什么来自例外)。关于e.what()的调用是否会做正确的事情,标准说什么?
?是否

保证工作,保证不工作或实施定义?


谢谢,

Dave

解决方案

Dave写道:

尝试
{
...
}
赶上(例外e)
{
cout<< e.what()<<在上面的代码中,e是按值而不是多态地捕获的(假设抛出的内容是从异常派生的)。标准对于e.what()的调用是否能做正确的事情说了什么?


你的意思是什么?正确的东西?

是否保证工作,保证不工作或实施定义?




''what''成员函数返回一些有效指针。显示它

就是它的用途。具体它将返回什么由实现定义

。如果您像这样捕获基类,它将返回适合转换和显示的内容,但不是

必然有意义。代码肯定不会调用派生的

class''" what()"会员。


V




" Victor Bazarov" <五******** @ comAcast.net>在消息中写道

新闻:2V ******************* @ newsread1.mlpsca01.us.t o.verio.net ... < blockquote class =post_quotes> Dave写道:

尝试
{
...
}
catch(例外e)
{
cout<< e.what()<<在上面的代码中,e被值捕获而不是多态
(假设抛出的内容是从异常派生的)。标准
说明对e.what()的调用是否会做正确的事情?
你对正确的事物的意思是什么?



通过正确的东西,我问它是否会返回一个指向字符串的指针

用于构造* derived *对象为反对指向

的其他字符串。它是否可以保证工作,保证不起作用或者确定实现是否定义了什么(?)

>是否保证工作,保证不工作或实现定义?



''what''成员函数返回一些有效指针。显示
就是它的用途。具体它将返回什么由实现定义。如果您像这样捕获基类,它将返回适合转换和显示的内容,但不一定有意义。代码肯定不会调用派生的
类''" what()"会员。

V



Dave写道:

" Victor Bazarov" <五******** @ comAcast.net>在消息中写道
新闻:2V ******************* @ newsread1.mlpsca01.us.t o.verio.net ...
< blockquote class =post_quotes> Dave写道:

尝试
{
...
}
catch(例外e)
{
cout<< e.what()<<在上面的代码中,e是按值而不是多态地捕获的(假设抛出的内容是从异常派生的)。标准
关于e.what()的调用是否能做正确的事情是什么意思?
你对正确的事情的意思是什么?


通过正确的东西,我问它是否会返回指向用于构造* derived * object

<的字符串
的指针br />

什么派生对象?传递什么字符串来构建它? std :: exception

*不知道*关于它们。

而不是指向
其他字符串。


当然它是指向其他字符串的指针。 ''std :: exception''没有

构造函数,它接受一个C字符串或一个C ++字符串。

它是否可以保证工作,保证不工作或者
实现定义了什么基本的what()被调用?




什么保证工作?


我写的上课:


班级基础{

公开:

基础();

虚拟〜 Base();

virtual int getnumber()const {return 42; }

};


世界上应该如何*我*知道*你*从中衍生出来并制作了

*你的*课程是用其他数字构建的,*你*

打算*你的*重写''getnumber''返回?


Just回答之前想一想。


V


try
{
...
}
catch (exception e)
{
cout << e.what() << endl;
}

In the code above, e is caught by value rather than polymorphically (assume
that what was thrown is derived from exception). What does the Standard say
about whether or not the call to e.what() will do the right thing? Is it
guaranteed to work, guaranteed to not work or implementation defined?

Thanks,
Dave

解决方案

Dave wrote:

try
{
...
}
catch (exception e)
{
cout << e.what() << endl;
}

In the code above, e is caught by value rather than polymorphically (assume
that what was thrown is derived from exception). What does the Standard say
about whether or not the call to e.what() will do the right thing?
What do you mean by "the right thing"?
Is it
guaranteed to work, guaranteed to not work or implementation defined?



The ''what'' member function returns some valid pointer. Displaying it
is what it''s there for. What specifically it will return is defined
by the implementation. If you catch the base class like this, it will
return something "suitable for conversion and display", but not
necessarily meaningful. The code will definitely not call the derived
class'' "what()" member.

V



"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:2V*******************@newsread1.mlpsca01.us.t o.verio.net...

Dave wrote:

try
{
...
}
catch (exception e)
{
cout << e.what() << endl;
}

In the code above, e is caught by value rather than polymorphically (assume that what was thrown is derived from exception). What does the Standard say about whether or not the call to e.what() will do the right thing?
What do you mean by "the right thing"?



By "the right thing", I''m asking if it will return a pointer to the string
that was used to construct the *derived* object as opposed to a pointer to
some other string. Is it guaranteed to work, guaranteed to not work or
implementation defined given that the base what() is being called?

> Is it
guaranteed to work, guaranteed to not work or implementation defined?



The ''what'' member function returns some valid pointer. Displaying it
is what it''s there for. What specifically it will return is defined
by the implementation. If you catch the base class like this, it will
return something "suitable for conversion and display", but not
necessarily meaningful. The code will definitely not call the derived
class'' "what()" member.

V



Dave wrote:

"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:2V*******************@newsread1.mlpsca01.us.t o.verio.net...

Dave wrote:

try
{
...
}
catch (exception e)
{
cout << e.what() << endl;
}

In the code above, e is caught by value rather than polymorphically
(assume
that what was thrown is derived from exception). What does the Standard
say
about whether or not the call to e.what() will do the right thing?
What do you mean by "the right thing"?


By "the right thing", I''m asking if it will return a pointer to the string
that was used to construct the *derived* object



What derived object? What string passed to construct it? std::exception
has *no idea* about them.
as opposed to a pointer to
some other string.
Of course it''s a pointer to some other string. ''std::exception'' has no
constructor that takes a C string or a C++ string.
Is it guaranteed to work, guaranteed to not work or
implementation defined given that the base what() is being called?



What guaranteed to work?

I write the class:

class Base {
public:
Base();
virtual ~Base();
virtual int getnumber() const { return 42; }
};

How in the world should *I* know that *you* derived from it and made
*your* class to be constructed with some other number, which *you*
intended to *your* overridden ''getnumber'' to return?

Just think a bit before replying.

V


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

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