调用可能抛出异常的函数 [英] Calling function that may throw an exception

查看:86
本文介绍了调用可能抛出异常的函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,我正在使用封装在类中的哈希表。

其中一个成员函数insert()抛出异常

如果插入失败(例如,如果该值已经存在于
$ b $中b哈希表)。现在我的客户端代码如下:


bool exception_thrown = false;


尝试

{

hash_table.insert(some_value);

}

catch(const std :: runtime_error& e)

{

std :: cerr<< e.what()<< std :: endl;


exception_thrown = true;

}


if(!exception_thrown)

{

std :: cout<< some_value<< "已成功插入。 <<的std :: ENDL; / *对于

调试目的,很高兴看到这里的位置* /

}


使用标志变量exception_thrown让我感到有点难看......我应该如何处理这个?b $ b?如果我正在调用一个函数,

可能会抛出异常,我想捕获该异常。如果我需要,我可以更改哈希值.b
表类本身。可能是insert()

成员函数应该返回一些错误代码而不是runtime_error

似乎更方便,因为它可以(我认为)

包含错误的描述。请指教。


/ Eric

Hello, I''m working with a hash table that is encapsulated in a class. One of
its member functions insert() throws an exception
if the insertion fails (for example, if the value was already present in the
hash table). Now I have client code that looks like this:

bool exception_thrown = false;

try
{
hash_table.insert(some_value);
}
catch(const std::runtime_error& e)
{
std::cerr << e.what() << std::endl;

exception_thrown = true;
}

if(!exception_thrown)
{
std::cout << some_value << " successfully inserted." << std::endl; /* For
debugging purposes, it would be nice to see position here */
}

Using the flag variable exception_thrown strikes me as a bit ugly...how
should I deal with this? If I''m calling a function that
may throw an exception I want to catch that exception. I can change the hash
table class itself if I need to..maybe the insert()
member function should return some error code instead but runtime_error
seems a bit more convenient since it can (should I think)
contain a description of the error. Please advise.

/ Eric

推荐答案

Eric Lilja写道:
Eric Lilja wrote:
您好,我正在使用封装在类中的哈希表。如果插入失败(例如,如果该值已经存在于
哈希表中),则其成员函数insert()之一会抛出异常
。现在我的客户端代码如下所示:

bool exception_thrown = false;

尝试
{
hash_table.insert(some_value);
}
catch(const std :: runtime_error& e)
{
std :: cerr<< e.what()<< std :: endl;

exception_thrown = true;
}
如果(!exception_thrown)
{
std :: cout< < some_value<< "已成功插入。 <<的std :: ENDL; / *对于
调试目的,很高兴看到这里的位置* /
}

使用标志变量exception_thrown让我觉得有点难看......怎么样?我应该处理这件事吗?如果我正在调用一个可以抛出异常的函数,我想要捕获该异常。我可以更改哈希表类本身,如果我需要..可能是insert()
成员函数应该返回一些错误代码但是runtime_error
似乎更方便,因为它可以(我应该认为)
包含错误的描述。请指教。
Hello, I''m working with a hash table that is encapsulated in a class. One of
its member functions insert() throws an exception
if the insertion fails (for example, if the value was already present in the
hash table). Now I have client code that looks like this:

bool exception_thrown = false;

try
{
hash_table.insert(some_value);
}
catch(const std::runtime_error& e)
{
std::cerr << e.what() << std::endl;

exception_thrown = true;
}

if(!exception_thrown)
{
std::cout << some_value << " successfully inserted." << std::endl; /* For
debugging purposes, it would be nice to see position here */
}

Using the flag variable exception_thrown strikes me as a bit ugly...how
should I deal with this? If I''m calling a function that
may throw an exception I want to catch that exception. I can change the hash
table class itself if I need to..maybe the insert()
member function should return some error code instead but runtime_error
seems a bit more convenient since it can (should I think)
contain a description of the error. Please advise.




最近在这里有一个讨论参数*反对*例外使用。

检查出来。 Herb Sutter的笔记提醒我们,一个

不应该将正常功能(及其处理)与

异常情况混淆。如果您的哈希表是_allowed_以表示

插入没有发生并且这是正常的,那么它必须是返回代码

而不是例外。无论如何,这样的东西。


V



There was a discussion here recently "Arguments *Against* Exception Use".
Check it out. There was the note by Herb Sutter reminding us that one
shouldn''t confuse normal functionality (and processing thereof) with
exceptional situations. If your hash table is _allowed_ to indicate that
insertion didn''t happen and that''s normal, then it has to be a return code
and not an exception. Something like that, anyway.

V


* Victor Bazarov:
* Victor Bazarov:
Eric Lilja写道:
Eric Lilja wrote:
您好,我正在使用封装在类中的哈希表。如果插入失败(例如,如果该值已经存在于
哈希表中),则其成员函数insert()之一会抛出异常
。现在我的客户端代码如下所示:

bool exception_thrown = false;

尝试
{
hash_table.insert(some_value);
}
catch(const std :: runtime_error& e)
{
std :: cerr<< e.what()<< std :: endl;

exception_thrown = true;
}
如果(!exception_thrown)
{
std :: cout< < some_value<< "已成功插入。 <<的std :: ENDL; / *对于
调试目的,很高兴看到这里的位置* /
}

使用标志变量exception_thrown让我觉得有点难看......怎么样?我应该处理这件事吗?如果我正在调用一个可以抛出异常的函数,我想要捕获该异常。我可以更改哈希表类本身,如果我需要..可能是insert()
成员函数应该返回一些错误代码但是runtime_error
似乎更方便,因为它可以(我应该认为)
包含错误的描述。请指教。
Hello, I''m working with a hash table that is encapsulated in a class. One of
its member functions insert() throws an exception
if the insertion fails (for example, if the value was already present in the
hash table). Now I have client code that looks like this:

bool exception_thrown = false;

try
{
hash_table.insert(some_value);
}
catch(const std::runtime_error& e)
{
std::cerr << e.what() << std::endl;

exception_thrown = true;
}

if(!exception_thrown)
{
std::cout << some_value << " successfully inserted." << std::endl; /* For
debugging purposes, it would be nice to see position here */
}

Using the flag variable exception_thrown strikes me as a bit ugly...how
should I deal with this? If I''m calling a function that
may throw an exception I want to catch that exception. I can change the hash
table class itself if I need to..maybe the insert()
member function should return some error code instead but runtime_error
seems a bit more convenient since it can (should I think)
contain a description of the error. Please advise.



最近在这里进行了讨论参数*反对*例外使用。
检查出来。 Herb Sutter的笔记提醒我们,一个
不应该混淆正常的功能(及其处理)和特殊情况。如果你的哈希表是_allowed_来表明
插入没有发生并且那是正常的,那么它必须是一个返回码
而不是例外。无论如何,这样的东西。



There was a discussion here recently "Arguments *Against* Exception Use".
Check it out. There was the note by Herb Sutter reminding us that one
shouldn''t confuse normal functionality (and processing thereof) with
exceptional situations. If your hash table is _allowed_ to indicate that
insertion didn''t happen and that''s normal, then it has to be a return code
and not an exception. Something like that, anyway.




两者都很容易。


在效率和效率方面简单性最好的是构建

异常 - 抛出一个作为返回代码的包装器。


但是我记得我至少成功地说过了一半一旦做了

,当更高层调用较低层软件时,情况恰恰相反。

原因是较低层的异常是什么? />
(例如无法发送邮件)在某个更高级别变为预期和

非常正常(例如向用户报告)。但是这个论点主要是用于拖钓目的,因为假设这两个

层都涉及相同的设计并不是一个有根据的... ... - )


-

答:因为它弄乱了人们通常阅读文字的顺序。

问:为什么是这是一件坏事吗?

答:热门发布。

问:usenet和电子邮件中最烦人的是什么?



It''s very easy to have both.

In terms of both efficiency and simplicity the best is to build the
exception-throwing one as a wrapper around the return-code one.

But I remember I argued at least halfway successfully once for doing
it the opposite way when a higher layer calls a lower layer of software.
The reason for that is that what is an exception at a lower layer
(e.g. unable to send mail) at some higher level becomes an expected and
quite normal thing (e.g. report that to the user). But the argument is
mostly for trolling purposes, because the assumption that both these
layers are involved in the same design is not a well-founded one... ;-)

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?




" Eric Lilja" < ER ******************* @ yahoo.com>在消息中写道

news:cn ********** @ news.island.liu.se ...

"Eric Lilja" <er*******************@yahoo.com> wrote in message
news:cn**********@news.island.liu.se...
你好,我在工作使用封装在类中的哈希表。如果插入失败(例如,如果哈希表中已经存在该值),其成员函数insert()中的一个将抛出异常
。现在我的客户端代码如下所示:

bool exception_thrown = false;

尝试
{
hash_table.insert(some_value);
}
catch(const std :: runtime_error& e)
{
std :: cerr<< e.what()<< std :: endl;

exception_thrown = true;
}
如果(!exception_thrown)
{
std :: cout< < some_value<< "已成功插入。 <<的std :: ENDL; / *
出于调试目的,很高兴看到这里的位置* /
}

使用标志变量exception_thrown让我觉得有点难看......怎么样?我应该处理这件事吗?


嗯,你当然可以简化代码,不需要旗帜


试试

{

hash_table.insert(some_value);

std :: cout<< some_value<< "已成功插入。 << std :: endl;

}

catch(const std :: runtime_error& e)

{

std :: cerr<< e.what()<< std :: endl;

}

如果我正在调用
可能会抛出异常的函数,我想捕获该异常。我可以更改
哈希表类本身,如果我需要..可能是insert()
成员函数应该返回一些错误代码但是runtime_error
似乎更方便,因为它可以(我应该认为)
包含错误的描述。请指教。
Hello, I''m working with a hash table that is encapsulated in a class. One
of its member functions insert() throws an exception
if the insertion fails (for example, if the value was already present in
the hash table). Now I have client code that looks like this:

bool exception_thrown = false;

try
{
hash_table.insert(some_value);
}
catch(const std::runtime_error& e)
{
std::cerr << e.what() << std::endl;

exception_thrown = true;
}

if(!exception_thrown)
{
std::cout << some_value << " successfully inserted." << std::endl; /*
For debugging purposes, it would be nice to see position here */
}

Using the flag variable exception_thrown strikes me as a bit ugly...how
should I deal with this?
Well you can certainly simplify the code, no flag is needed

try
{
hash_table.insert(some_value);
std::cout << some_value << " successfully inserted." << std::endl;
}
catch(const std::runtime_error& e)
{
std::cerr << e.what() << std::endl;
}
If I''m calling a function that
may throw an exception I want to catch that exception. I can change the
hash table class itself if I need to..maybe the insert()
member function should return some error code instead but runtime_error
seems a bit more convenient since it can (should I think)
contain a description of the error. Please advise.




insert()可能会返回一个包含错误消息的状态对象,如果

出错了。


john



insert() could return a status object that contains the error message if
things go wrong.

john


这篇关于调用可能抛出异常的函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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