何时使用例外 [英] When to use exceptions
问题描述
我想要做的是从fstream以小端形式读取一个32位无符号整数(让我们称之为
u32)。如果我的
函数是这样的话会更好吗:
//如果读取值时发生错误则返回false
bool read(std :: fstream& stream,u32& value);
或者这个:
u32 read(std :: fstream& stream)抛出(FileReadingException);
一般来说,什么是一些很好的指导方针,当首先像
这样的东西比第二个更喜欢反之亦然?
-
我只是海市蜃楼。
What I want to do is to read a 32-bit unsigned integer (let''s call that
u32) in little-endian form from an fstream. Would it be better if my
function went like this:
// returns false if an error occurs reading the value
bool read(std::fstream& stream, u32& value);
or this:
u32 read(std::fstream& stream) throw(FileReadingException);
In general, what are some good guidelines for when something like the
first is preferred over something like the second, and vice-versa?
--
I am only a mirage.
推荐答案
* kelvSYC:
* kelvSYC:
我想要做的是从fstream以小端形式读取一个32位无符号整数(让我们称之为u32)。如果我的
函数如下所示会更好:
如果读取值时发生错误则返回false
bool read(std :: fstream& stream,u32&值);
或者:u32 read(std :: fstream& stream)throw(FileReadingException);
只有这两个可供选择:
第一个也是唯一第一个。
但是也提供像第二个这样的抛出版本,但毫无例外
规格。
一般来说,什么是什么样的好指南什么时候像
What I want to do is to read a 32-bit unsigned integer (let''s call that
u32) in little-endian form from an fstream. Would it be better if my
function went like this:
// returns false if an error occurs reading the value
bool read(std::fstream& stream, u32& value);
or this:
u32 read(std::fstream& stream) throw(FileReadingException);
Given only these two to choose from:
the first and only the first.
But also provide a throwing version like the second but without exception
specification.
In general, what are some good guidelines for when something like the
first is preferred over something like the second
始终。在
C ++中,异常规范是非常不切实际和误导的。在某些情况下,只有少数表格是可以接受的。
-
答:因为它弄乱了人们通常阅读文字的顺序。
问:为什么这么糟糕?
A:热门发布。
问:什么是最烦人的事情usenet和电子邮件?
Always. Exception specifications are very impractical and misleading in
C++. There are only a few forms that are acceptable, in certain contexts.
--
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?
> > //如果发生错误,则返回false,读取值
> > // returns false if an error occurs reading the value
bool read(std :: fstream& stream,u32& value);
或者这个:
u32 read(std :: fstream& stream)throw(FileReadingException);
bool read(std::fstream& stream, u32& value);
or this:
u32 read(std::fstream& stream) throw(FileReadingException);
只有这两个可供选择:
第一个也是唯一的第一个。
但也提供像第二个这样的投掷版本,但无一例外
规范。
Given only these two to choose from:
the first and only the first.
But also provide a throwing version like the second but without exception
specification.
所以这样的压缩:
//如果读取值时发生错误则抛出异常,但不是原型中指定的
//
u32 read(std :: fstream& stream);
可以接受C ++风格吗?
-
我只是海市蜃楼。
So a comprimise like so:
// throws exception if an error occurs reading the value, but is not
// specified in the prototype
u32 read(std::fstream& stream);
would be acceptable C++ style then?
--
I am only a mirage.
* kelvSYC:
* kelvSYC:
//如果发生错误,则返回false g值
bool read(std :: fstream&流,u32&值);
或者:
u32读取(std :: fstream& stream)throw(FileReadingException);
// returns false if an error occurs reading the value
bool read(std::fstream& stream, u32& value);
or this:
u32 read(std::fstream& stream) throw(FileReadingException);
给定只有这两个可供选择:
第一个也是唯一的第一个。
但也提供像第二个但无一例外的投掷版本
规范。
Given only these two to choose from:
the first and only the first.
But also provide a throwing version like the second but without exception
specification.
所以这样的压缩:
//如果在读取值时发生错误,则抛出异常,但在原型中没有指定// br /> u32读取(std :: fstream& stream);
是否可以接受C ++风格呢?
So a comprimise like so:
// throws exception if an error occurs reading the value, but is not
// specified in the prototype
u32 read(std::fstream& stream);
would be acceptable C++ style then?
1.如果你怎么办?需要i32结果,双重结果等等吗?
2.如果你想将它用于一个不是fstream的流?怎么办?
3.早先的窘境似乎表明非投掷版本可以派上用场,而且没有必要将它排除在外。
所以也许
模板< typename T>
void throwIf0(T nonzero,char const diagnostic [])
{
if(!nonzero){throw std: :runtime_error(诊断); }
}
bool readBinary(std :: istream& stream,u32& result){...}
u32 u32FromBinary(std :: istream& stream)
{
u32 result;
throwIf0(read(stream,result), 我有用的诊断);
返回结果;
}
对于文本我只读文本字符串并将它们转换为它们代表的任何
,因为这些函数更清晰,更可靠,并且因为基于流的文本到其他转换不是
在C ++中定义良好(界面在那里,定义没有)。
-
答:因为它弄乱了订单人们通常会阅读文字。
问:为什么这么糟糕?
A:热门帖子。
问:什么usenet和电子邮件中最烦人的事情是什么?
1. What if you need i32 result, double result, and so forth?
2. What if you''d like to use this for a stream that''s not an fstream?
3. The earlier quandary seems to indicate a non-throwing version can
come in handy, and there''s no need to exclude it.
So perhaps
template< typename T >
void throwIf0( T nonzero, char const diagnostic[] )
{
if( !nonzero ){ throw std::runtime_error( diagnostic ); }
}
bool readBinary( std::istream& stream, u32& result ){ ... }
u32 u32FromBinary( std::istream& stream )
{
u32 result;
throwIf0( read( stream, result ), "my useful diagnostic" );
return result;
}
For text I''d just read text strings and convert them to whatever
they represent, because such functions are much cleaner and more
reusable, and because stream based text-to-other conversion is not
well-defined in C++ (the interface is there, the definition not).
--
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?
这篇关于何时使用例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!