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.
|
|
|
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
|
|
|
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.
|