由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
Programming版 - 一道编程题 - throw Exceptions
相关主题
C++ Q15: throw请教一个c++ throw exception 问题
c++ exception为什么用try catch不住exception?
几个问题C++: exception: out-of-order execution?
C++ 用户定义exception的标准用法是什么?有大侠讲讲RTTI和exception的问题么?
大家对 exception 都是怎么处理的?问一个constructor的问题 (转载)
请教一下,exception时,destructor一定会被调用么?C++ exception handling 一问
Two questions about handling exceptions in C++C++ Exception Question
Java的例外处理问题什么情况下不用写throwsc++:exception 一问
相关话题的讨论汇总
话题: exception话题: throw话题: exceptions话题: error
进入Programming版参与讨论
1 (共1页)
c*******1
发帖数: 1
1
问下面哪种情况需要throw Exceptions,为什么?
1。set the capacity of a Vector to a negative value
2。一个object设置初始状态的configuration file有语法错误
3。一个在string array中搜索word的method无法找到该word
g*****g
发帖数: 34805
2

1 and 2

【在 c*******1 的大作中提到】
: 问下面哪种情况需要throw Exceptions,为什么?
: 1。set the capacity of a Vector to a negative value
: 2。一个object设置初始状态的configuration file有语法错误
: 3。一个在string array中搜索word的method无法找到该word

q*********9
发帖数: 8
3
Exceptions should only be thrown under exceptional conditions, in all the
cases, you can return some error message to indicate error. I don't
understand why we must( or better ) throw exception?
goodbug, would you explain ?
Thanks,
c*****t
发帖数: 1879
4
goodbug's answer is merely for general cases, not for specific senarios.
It is quite annoying to handle a lot of checked exceptions sometimes.
Unchecked exception is dangerous if not handled. Returning error code
is pretty much like checked exception, only worse since one has to check
the error. If you are going to deal with each specific error any ways,
returning error code is better.
So take-n-pick the best thing for you.

【在 q*********9 的大作中提到】
: Exceptions should only be thrown under exceptional conditions, in all the
: cases, you can return some error message to indicate error. I don't
: understand why we must( or better ) throw exception?
: goodbug, would you explain ?
: Thanks,

g*****g
发帖数: 34805
5
You throw exception whenever it's exceptional conditions.
Negative size is exceptional, improper configuration is exceptional
too. Exception gives caller a chance to handle the error. The processing
can be as simple as log and ignore, but should be throwed as long as the
callee cannot handle it.

【在 q*********9 的大作中提到】
: Exceptions should only be thrown under exceptional conditions, in all the
: cases, you can return some error message to indicate error. I don't
: understand why we must( or better ) throw exception?
: goodbug, would you explain ?
: Thanks,

q*********9
发帖数: 8
6
Need some clarification for case 2: normally an initial configuration is
called within constructor, so we should throw and catch the exception inside
the constructor instead of throwing out of it. Is this what you mean for
case 2?
g*****g
发帖数: 34805
7
Depends on how serious you think the error is.
You may want to throw out of constructor to halt
the application in some case.

inside

【在 q*********9 的大作中提到】
: Need some clarification for case 2: normally an initial configuration is
: called within constructor, so we should throw and catch the exception inside
: the constructor instead of throwing out of it. Is this what you mean for
: case 2?

E*V
发帖数: 17544
8
agree with you

【在 g*****g 的大作中提到】
: You throw exception whenever it's exceptional conditions.
: Negative size is exceptional, improper configuration is exceptional
: too. Exception gives caller a chance to handle the error. The processing
: can be as simple as log and ignore, but should be throwed as long as the
: callee cannot handle it.

q*********9
发帖数: 8
9
Thanks, now I am going back to case 1.
Why it's better to throw exception inside set function? (Suppose that's the
reason why the question is asked).
I would gladly accept it if we assume this set function actually allocates
memory.
g*****g
发帖数: 34805
10
You can't allocate negative memory, what else you want to do?

the
allocates

【在 q*********9 的大作中提到】
: Thanks, now I am going back to case 1.
: Why it's better to throw exception inside set function? (Suppose that's the
: reason why the question is asked).
: I would gladly accept it if we assume this set function actually allocates
: memory.

相关主题
请教一下,exception时,destructor一定会被调用么?请教一个c++ throw exception 问题
Two questions about handling exceptions in C++为什么用try catch不住exception?
Java的例外处理问题什么情况下不用写throwsC++: exception: out-of-order execution?
进入Programming版参与讨论
q*********9
发帖数: 8
11
can we keep the current capacity when input is negative, but throw exception
when client uses elements exceeding capacity?
Won't this save us one exception since we'll always throw when out-of-bound?
E*V
发帖数: 17544
12
你是球星的马甲?

exception
bound?

【在 q*********9 的大作中提到】
: can we keep the current capacity when input is negative, but throw exception
: when client uses elements exceeding capacity?
: Won't this save us one exception since we'll always throw when out-of-bound?

g*****g
发帖数: 34805
13
exception is cheap, why do you want to hide an error?

exception
bound?

【在 q*********9 的大作中提到】
: can we keep the current capacity when input is negative, but throw exception
: when client uses elements exceeding capacity?
: Won't this save us one exception since we'll always throw when out-of-bound?

q*********9
发帖数: 8
14
To: EUV
No, who's 球星:)
Thanks goodbug, Although I am involving in a big system but I don't see the
handling of exceptions, sometimes it's hard to decide the pros and cons with
exception. I am wondering who's really using it?
E*V
发帖数: 17544
15
球星以前是linux的常客,最近很忙不太见了

the
with

【在 q*********9 的大作中提到】
: To: EUV
: No, who's 球星:)
: Thanks goodbug, Although I am involving in a big system but I don't see the
: handling of exceptions, sometimes it's hard to decide the pros and cons with
: exception. I am wondering who's really using it?

q*********9
发帖数: 8
16
I see, I will stay here for a while, trying to learn from you guys.
E*V
发帖数: 17544
17
如果你用linux,欢迎去linux版

【在 q*********9 的大作中提到】
: I see, I will stay here for a while, trying to learn from you guys.
q*********9
发帖数: 8
18
sure, are you ban zhu in 'linux'?
E*V
发帖数: 17544
19
en

【在 q*********9 的大作中提到】
: sure, are you ban zhu in 'linux'?
T*****9
发帖数: 2484
20
除了3都需要

【在 c*******1 的大作中提到】
: 问下面哪种情况需要throw Exceptions,为什么?
: 1。set the capacity of a Vector to a negative value
: 2。一个object设置初始状态的configuration file有语法错误
: 3。一个在string array中搜索word的method无法找到该word

相关主题
有大侠讲讲RTTI和exception的问题么?C++ Exception Question
问一个constructor的问题 (转载)c++:exception 一问
C++ exception handling 一问A try-catch problem in C++
进入Programming版参与讨论
s******n
发帖数: 876
21
"fail fast"

exception
bound?

【在 q*********9 的大作中提到】
: can we keep the current capacity when input is negative, but throw exception
: when client uses elements exceeding capacity?
: Won't this save us one exception since we'll always throw when out-of-bound?

k****i
发帖数: 1072
22
All your other statemens are right, but exception is expensive.

【在 g*****g 的大作中提到】
: exception is cheap, why do you want to hide an error?
:
: exception
: bound?

B********e
发帖数: 1062
23
Tolerance is different. Goodbug is a fan of Java.

【在 k****i 的大作中提到】
: All your other statemens are right, but exception is expensive.
1 (共1页)
进入Programming版参与讨论
相关主题
c++:exception 一问大家对 exception 都是怎么处理的?
A try-catch problem in C++请教一下,exception时,destructor一定会被调用么?
c++ exception 一问Two questions about handling exceptions in C++
请教:函数后面的 throw() 有意义么?Java的例外处理问题什么情况下不用写throws
C++ Q15: throw请教一个c++ throw exception 问题
c++ exception为什么用try catch不住exception?
几个问题C++: exception: out-of-order execution?
C++ 用户定义exception的标准用法是什么?有大侠讲讲RTTI和exception的问题么?
相关话题的讨论汇总
话题: exception话题: throw话题: exceptions话题: error