[设计]访问者应该抛出异常吗? [英] [Design] Should accessors throw exception ?

查看:48
本文介绍了[设计]访问者应该抛出异常吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,


我想知道以下

问题是否有一般答案:制作公共界面时访问一个容器

该函数在未找到该元素时是否会抛出异常?

否则我需要两个函数:一个Find和一个Get函数,它放在

应用程序员总是打电话查找之前的负担

获取。

另一种选择是返回某种哨兵(如

std :: set<> :: end())...


评论?


谢谢

-Mathieu


例如。

#include< map>

#include< string.h> ;


struct Phone {int P;
struct Name {const char * N;

bool operator<(Name const& n)const {return strcmp(N,n.N)< 0; }

};

struct黄页

{

void Insert(Name const& n,Phone const& ; p)

{I.insert(std :: map< Name,Phone> :: value_type(n,p)); }

bool Find(Name const& n){return I.find(n)!= I.end(); }

电话const& Get(姓名const& n){return I.find(n) - > second; }

私人:

std :: map<姓名,电话I;

};

Hi there,

I''d like to know if there is a general answer to the following
question: when making a public interface that access a container
should the function throw an exception when the element is not found ?
Otherwise I need two functions: a Find and a Get function which put
the burden on the application programmer to always call Find before
Get.
The other alternative would be to return some kind of sentinel (like
std::set<>::end() )...

Comments ?

Thanks
-Mathieu

eg.
#include <map>
#include <string.h>

struct Phone { int P; };
struct Name { const char *N;
bool operator <(Name const &n) const { return strcmp(N,n.N) < 0; }
};
struct YellowPage
{
void Insert(Name const &n,Phone const &p)
{ I.insert( std::map<Name,Phone>::value_type(n,p) ); }
bool Find(Name const & n) { return I.find(n) != I.end(); }
Phone const &Get(Name const&n) { return I.find(n)->second; }
private:
std::map<Name,PhoneI;
};

推荐答案




我认为返回一个特殊值,如end()或0指针是最好的

解决方案。


我认为异常只应在异常发生时使用,对于

实例,当你试图打开时,你希望有一个配置文件/>
它丢失了或者您尝试访问数据库但是DBA将其删除

进行维护。通过这种方式,你(或者你的图书馆的用户)可以在代码中的某个地方高起来捕获那些知道介于

之间的所有东西被释放和处理的东西很好地继续(也许等待几分钟再次尝试
再试一次。


确实一个find / get组合更多的工作。还有结束()你制作

它和STL一样工作。


问候,Ron AF Greve

http://www.InformationSuperHighway.eu


" mathieu"< ma***************@gmail.com在留言中写道

news:32 ***************** ***************** @ k13g2000 hse.googlegroups.com ...
Hi,

I think returning a special value like end() or 0 for a pointer is the best
solution.

I think exception should only be used when something exceptional occurs, for
instance you expect a conifiguration file is there yet when you try to open
it it is missing or you try to access a database but the DBA took it down
for maintenance. This way you (or the user of your library) can catch those
thiings somewhere ''high up'' in the code knowing that everything in between
is released and handled nicely and continue (maybe waiting a few minutes and
try again.

And indeed a find/get combination is more work. Also with the end() you make
it work the same as the STL.

Regards, Ron AF Greve

http://www.InformationSuperHighway.eu

"mathieu" <ma***************@gmail.comwrote in message
news:32**********************************@k13g2000 hse.googlegroups.com...

你好,


我想知道以下

问题是否有一般答案:制作访问容器的公共界面时

应该是函数t在找不到元素时发现异常?

否则我需要两个函数:一个Find和一个Get函数,它给应用程序员总是调用Find带来了负担之前

获取。

另一种选择是返回某种哨兵(比如

std :: set<> :: end ())...


评论?


谢谢

-Mathieu

例如。


#include< map>

#include< string.h>


struct Phone {int P;
struct Name {const char * N;

bool operator<(Name const& n)const {return strcmp(N,n.N)< 0; }

};

struct黄页

{

void Insert(Name const& n,Phone const& ; p)

{I.insert(std :: map< Name,Phone> :: value_type(n,p)); }

bool Find(Name const& n){return I.find(n)!= I.end(); }

电话const& Get(姓名const& n){return I.find(n) - > second; }

private:

std :: map< Name,PhoneI;

};
Hi there,

I''d like to know if there is a general answer to the following
question: when making a public interface that access a container
should the function throw an exception when the element is not found ?
Otherwise I need two functions: a Find and a Get function which put
the burden on the application programmer to always call Find before
Get.
The other alternative would be to return some kind of sentinel (like
std::set<>::end() )...

Comments ?

Thanks
-Mathieu

eg.
#include <map>
#include <string.h>

struct Phone { int P; };
struct Name { const char *N;
bool operator <(Name const &n) const { return strcmp(N,n.N) < 0; }
};
struct YellowPage
{
void Insert(Name const &n,Phone const &p)
{ I.insert( std::map<Name,Phone>::value_type(n,p) ); }
bool Find(Name const & n) { return I.find(n) != I.end(); }
Phone const &Get(Name const&n) { return I.find(n)->second; }
private:
std::map<Name,PhoneI;
};



mathieu写道:
mathieu wrote:

你好,


我想知道以下

问题是否有一般答案:制作访问容器的公共界面时

该函数是否应该在找不到该元素时抛出异常?

否则我需要两个函数:一个Find和一个Get函数,它将

给应用程序程序员带来了负担总是先打电话查询

获取。
Hi there,

I''d like to know if there is a general answer to the following
question: when making a public interface that access a container
should the function throw an exception when the element is not found ?
Otherwise I need two functions: a Find and a Get function which put
the burden on the application programmer to always call Find before
Get.



如果返回类型是指针,我倾向于返回NULL,如果返回类型是引用,则抛出异常
对象

没有明显的坏价值。


-

Ian Collins。

I tend to favour returning NULL if the return type is a pointer and
throwing an exception if the return type is a reference or an object
without an obvious bad value.

--
Ian Collins.


9月12日晚上11点07分,mathieu< mathieu.malate ... @ gmail.comwrote:
On Sep 12, 11:07 pm, mathieu <mathieu.malate...@gmail.comwrote:

我想要知道以下是否有一般性答案

问题:当创建一个访问

容器的公共接口时该函数会在
$时抛出异常找不到b $ b元素?否则我需要两个函数:一个查找

和一个Get函数,它会给应用程序带来负担

程序员总是在Get之前调用Find。
I''d like to know if there is a general answer to the following
question: when making a public interface that access a
container should the function throw an exception when the
element is not found ? Otherwise I need two functions: a Find
and a Get function which put the burden on the application
programmer to always call Find before Get.



这取决于应用程序。我在我的预标准AssocArray(哈希表)中使用了两种不同的解决方案

:运算符[]有元素存在的前提条件,并且声明了这一点

前置条件,但是有一个get函数返回了一个

指向该元素的指针,如果

元素是'n'则返回一个空指针找不到。在其他情况下,我发现std :: map<> :: operator []的

行为也很有用:只需

插入缺失元件。另一方面,我不能想到

a上下文,抛出异常将是所希望的行为。

It depends on the application. I used two different solutions
in my pre-standard AssocArray (a hash table): the operator[] had
a pre-condition that the element was present, and asserted this
precondition, but there was a get function which returned a
pointer to the element, and returned a null pointer if the
element wasn''t found. In other contexts, I''ve found the
behavior of std::map<>::operator[] to be useful as well: just
insert the missing element. On the other hand, I can''t think of
a context where throwing an exception would be the desired
behavior.


另一种选择是返回某种哨兵

(如std :: set<> :: end())...
The other alternative would be to return some kind of sentinel
(like std::set<>::end() )...



只需返回指向元素的指针,而不是引用,

并且你有一个由标准定义的哨兵值,null

指针。


-

James Kanze(GABI软件)电子邮件:ja ********* @ gmail.com

Conseils eninformatiqueorientéeobjet/

Beratung in objektorientierter Datenverarbeitung

9placeSémard,78210 St.-Cyr-l''coco,France,+ 33( 0)1 30 23 00 34

Just return a pointer to the element, rather than a reference,
and you have a sentinel value defined by the standard, the null
pointer.

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l''école, France, +33 (0)1 30 23 00 34


这篇关于[设计]访问者应该抛出异常吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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