Ognacの雑感

木漏れ日々

目次

Blog 利用状況

書庫

ギャラリ

正常系のError処理と異常系のError処理

Error発生時は例外を投げて,回収する。この事自体は正解なのですが,
なんでも例外投げで良いのでしょうか? 最近疑問に感じています。

-------例
    Public Function div(ByVal a As Integer, ByVal b As Integer) As Integer
        Dim ans As Integer = 0
        Try
            ans = a \ b
        Catch ex As Exception
            Throw
        End Try
        Return ans
    End Function
---------
 b = 0のとき Divide by 0 が発生します。
 しかし, 引数の意味からみると, b=0 で呼ばれるかも知れないことは十分想像がつくことです.
 この場合は ,正常系として処理すべきと思います。
   if b=0 then  return xx:   戻り値の xxの設定は別次元の話.
 業務ロジック上,b=0 が在り得ないならば, assertでコーディングErrorとすべきだと思います。

正常系の異常値は戻り値処理し, 本来のアブノーマルな状態のみ例外を投げるべきだと最近思っています。


 

投稿日時 : 2006年9月22日 1:01

Feedback

# re: 正常系のError処理と異常系のError処理 2006/09/22 1:33 かるあ

同意です。
正常系の異常値というか、復帰可能な(自分が処理出来る)例外だった場合は戻り値で処理するべきですよね。

# re: 正常系のError処理と異常系のError処理 2006/09/22 2:32 囚人

ここは例外をスローするべきシナリオかな、というのが私の意見です。もちろん場合によりますけども。
Public な div というメソッド名から「割り算をする」事が目的のメソッドなので、b に 0 を渡されても処理の続行が不可能です。

If b = 0 Then Throw new ArgumentOutOfRangeException("b に 0 は駄目だよ!")

で良いのです。0 を「渡さない責任」があるのは div のクライアントです。
戻り値で誤魔化して問題を先送りにするのは問題です。

# re: 正常系のError処理と異常系のError処理 2006/09/22 11:59 ognac

例外で対処すべき......仰られるとそのような気もする...優柔不断ですねww
確かに示した例では DIV(a,b) になっているので,意味合いから b=0 でCallするのは異常な使用ですので,例外発生で良いですね。

--例2
Public Function Ave(ByVal a As ArrayList) As Integer
Dim sum As Integer = 0
For Each i As Integer In a
sum += i
Next
Return sum \ a.Count
End Function
--
Arraylistの平均を求めるMethodの場合を考えます。
該当する要素数が0個で呼ぶことはありがちなことだと思えます。
if a.count = 0 then return 0
その際は, 0を戻すのが理に適っていると考えます.

>0 を「渡さない責任」があるのは div のクライアントです。
>戻り値で誤魔化して問題を先送りにするのは問題です。

納得です. ケースバイケースですが,コントロールを切って区切り単位に平均を算出する場合は,
要素数0の対処は ave(a) 側で実装した方がすっきりする気もします。
今回も結論が揺らいでしまいました。


#  業務上起こりえない判定ルーチンを書くのは冗長? 2008/01/31 10:36 Ognacの雑感

業務上起こりえない判定ルーチンを書くのは冗長?

# I got this site from my pal who told me about this web site and at the moment this time I am visiting this web page and reading very informative content here. 2021/08/29 0:13 I got this site from my pal who told me about this

I got this site from my pal who told me about this web site and at the moment this time I am visiting this web page and reading very informative content here.

# I got this site from my pal who told me about this web site and at the moment this time I am visiting this web page and reading very informative content here. 2021/08/29 0:14 I got this site from my pal who told me about this

I got this site from my pal who told me about this web site and at the moment this time I am visiting this web page and reading very informative content here.

# I got this site from my pal who told me about this web site and at the moment this time I am visiting this web page and reading very informative content here. 2021/08/29 0:15 I got this site from my pal who told me about this

I got this site from my pal who told me about this web site and at the moment this time I am visiting this web page and reading very informative content here.

# I got this site from my pal who told me about this web site and at the moment this time I am visiting this web page and reading very informative content here. 2021/08/29 0:16 I got this site from my pal who told me about this

I got this site from my pal who told me about this web site and at the moment this time I am visiting this web page and reading very informative content here.

# I think this is one of the most important information for me. And i'm glad reading your article. But should remark on few general things, The website style is ideal, the articles is really great : D. Good job, cheers 2021/08/30 5:20 I think this is one of the most important informat

I think this is one of the most important information for me.
And i'm glad reading your article. But should remark on few general things, The website style is ideal, the articles is really
great : D. Good job, cheers

# I think this is one of the most important information for me. And i'm glad reading your article. But should remark on few general things, The website style is ideal, the articles is really great : D. Good job, cheers 2021/08/30 5:21 I think this is one of the most important informat

I think this is one of the most important information for me.
And i'm glad reading your article. But should remark on few general things, The website style is ideal, the articles is really
great : D. Good job, cheers

# I think this is one of the most important information for me. And i'm glad reading your article. But should remark on few general things, The website style is ideal, the articles is really great : D. Good job, cheers 2021/08/30 5:22 I think this is one of the most important informat

I think this is one of the most important information for me.
And i'm glad reading your article. But should remark on few general things, The website style is ideal, the articles is really
great : D. Good job, cheers

# I think this is one of the most important information for me. And i'm glad reading your article. But should remark on few general things, The website style is ideal, the articles is really great : D. Good job, cheers 2021/08/30 5:23 I think this is one of the most important informat

I think this is one of the most important information for me.
And i'm glad reading your article. But should remark on few general things, The website style is ideal, the articles is really
great : D. Good job, cheers

# What's up, the whole thing is going sound here and ofcourse every one is sharing data, that's truly good, keep up writing. 2021/09/02 5:35 What's up, the whole thing is going sound here and

What's up, the whole thing is going sound here and ofcourse every one is sharing data,
that's truly good, keep up writing.

# What's up, the whole thing is going sound here and ofcourse every one is sharing data, that's truly good, keep up writing. 2021/09/02 5:36 What's up, the whole thing is going sound here and

What's up, the whole thing is going sound here and ofcourse every one is sharing data,
that's truly good, keep up writing.

# What's up, the whole thing is going sound here and ofcourse every one is sharing data, that's truly good, keep up writing. 2021/09/02 5:37 What's up, the whole thing is going sound here and

What's up, the whole thing is going sound here and ofcourse every one is sharing data,
that's truly good, keep up writing.

# What's up, the whole thing is going sound here and ofcourse every one is sharing data, that's truly good, keep up writing. 2021/09/02 5:38 What's up, the whole thing is going sound here and

What's up, the whole thing is going sound here and ofcourse every one is sharing data,
that's truly good, keep up writing.

# I'll immediately seize your rss as I can't find your e-mail subscription link or e-newsletter service. Do you've any? Kindly allow me know in order that I could subscribe. Thanks. 2021/09/04 21:02 I'll immediately seize your rss as I can't find y

I'll immediately seize your rss as I can't find your e-mail
subscription link or e-newsletter service.

Do you've any? Kindly allow me know in order that I could subscribe.
Thanks.

# I'll immediately seize your rss as I can't find your e-mail subscription link or e-newsletter service. Do you've any? Kindly allow me know in order that I could subscribe. Thanks. 2021/09/04 21:03 I'll immediately seize your rss as I can't find y

I'll immediately seize your rss as I can't find your e-mail
subscription link or e-newsletter service.

Do you've any? Kindly allow me know in order that I could subscribe.
Thanks.

# I'll immediately seize your rss as I can't find your e-mail subscription link or e-newsletter service. Do you've any? Kindly allow me know in order that I could subscribe. Thanks. 2021/09/04 21:04 I'll immediately seize your rss as I can't find y

I'll immediately seize your rss as I can't find your e-mail
subscription link or e-newsletter service.

Do you've any? Kindly allow me know in order that I could subscribe.
Thanks.

# I'll immediately seize your rss as I can't find your e-mail subscription link or e-newsletter service. Do you've any? Kindly allow me know in order that I could subscribe. Thanks. 2021/09/04 21:05 I'll immediately seize your rss as I can't find y

I'll immediately seize your rss as I can't find your e-mail
subscription link or e-newsletter service.

Do you've any? Kindly allow me know in order that I could subscribe.
Thanks.

# Good article! We will be linking to this particularly great post on our website. Keep up the good writing. 2021/09/05 19:36 Good article! We will be linking to this particula

Good article! We will be linking to this particularly great post on our website.

Keep up the good writing.

# Good article! We will be linking to this particularly great post on our website. Keep up the good writing. 2021/09/05 19:37 Good article! We will be linking to this particula

Good article! We will be linking to this particularly great post on our website.

Keep up the good writing.

# Hi there friends, how is everything, and what you wish for to say concerning this post, in my view its truly awesome designed for me. 2021/09/06 1:29 Hi there friends, how is everything, and what you

Hi there friends, how is everything, and what you wish for to
say concerning this post, in my view its truly awesome designed for me.

# Hi there friends, how is everything, and what you wish for to say concerning this post, in my view its truly awesome designed for me. 2021/09/06 1:30 Hi there friends, how is everything, and what you

Hi there friends, how is everything, and what you wish for to
say concerning this post, in my view its truly awesome designed for me.

# Hi there friends, how is everything, and what you wish for to say concerning this post, in my view its truly awesome designed for me. 2021/09/06 1:31 Hi there friends, how is everything, and what you

Hi there friends, how is everything, and what you wish for to
say concerning this post, in my view its truly awesome designed for me.

# Hi there friends, how is everything, and what you wish for to say concerning this post, in my view its truly awesome designed for me. 2021/09/06 1:32 Hi there friends, how is everything, and what you

Hi there friends, how is everything, and what you wish for to
say concerning this post, in my view its truly awesome designed for me.

# Everyone loves what you guys tend to be up too. This kind of clever work and coverage! Keep up the amazing works guys I've you guys to my own blogroll. 2021/10/26 16:58 Everyone loves what you guys tend to be up too. Th

Everyone loves what you guys tend to be up too. This kind of
clever work and coverage! Keep up the amazing works guys I've
you guys to my own blogroll.

# Everyone loves what you guys tend to be up too. This kind of clever work and coverage! Keep up the amazing works guys I've you guys to my own blogroll. 2021/10/26 16:59 Everyone loves what you guys tend to be up too. Th

Everyone loves what you guys tend to be up too. This kind of
clever work and coverage! Keep up the amazing works guys I've
you guys to my own blogroll.

# Everyone loves what you guys tend to be up too. This kind of clever work and coverage! Keep up the amazing works guys I've you guys to my own blogroll. 2021/10/26 17:00 Everyone loves what you guys tend to be up too. Th

Everyone loves what you guys tend to be up too. This kind of
clever work and coverage! Keep up the amazing works guys I've
you guys to my own blogroll.

# Everyone loves what you guys tend to be up too. This kind of clever work and coverage! Keep up the amazing works guys I've you guys to my own blogroll. 2021/10/26 17:01 Everyone loves what you guys tend to be up too. Th

Everyone loves what you guys tend to be up too. This kind of
clever work and coverage! Keep up the amazing works guys I've
you guys to my own blogroll.

# Hi! I know this is sort of off-topic however I had to ask. Does running a well-established blog like yours take a large amount of work? I'm completely new to running a blog however I do write in my diary daily. I'd like to start a blog so I can easily s 2021/11/25 7:28 Hi! I know this is sort of off-topic however I had

Hi! I know this is sort of off-topic however I had to ask.
Does running a well-established blog like yours take a large amount of
work? I'm completely new to running a blog however I do
write in my diary daily. I'd like to start a blog so I can easily share my experience and feelings online.
Please let me know if you have any kind of recommendations or
tips for new aspiring bloggers. Appreciate it!

# Hello, i think that i saw you visited my web site thus i came to “return the favor”.I am trying to find things to improve my web site!I suppose its ok to use a few of your ideas!! 2022/03/23 17:33 Hello, i think that i saw you visited my web site

Hello, i think that i saw you visited my web site thus i came to
“return the favor”.I am trying to find things to improve
my web site!I suppose its ok to use a few of your ideas!!

# Hello, i think that i saw you visited my web site thus i came to “return the favor”.I am trying to find things to improve my web site!I suppose its ok to use a few of your ideas!! 2022/03/23 17:34 Hello, i think that i saw you visited my web site

Hello, i think that i saw you visited my web site thus i came to
“return the favor”.I am trying to find things to improve
my web site!I suppose its ok to use a few of your ideas!!

# Hello, i think that i saw you visited my web site thus i came to “return the favor”.I am trying to find things to improve my web site!I suppose its ok to use a few of your ideas!! 2022/03/23 17:35 Hello, i think that i saw you visited my web site

Hello, i think that i saw you visited my web site thus i came to
“return the favor”.I am trying to find things to improve
my web site!I suppose its ok to use a few of your ideas!!

# Hello, i think that i saw you visited my web site thus i came to “return the favor”.I am trying to find things to improve my web site!I suppose its ok to use a few of your ideas!! 2022/03/23 17:36 Hello, i think that i saw you visited my web site

Hello, i think that i saw you visited my web site thus i came to
“return the favor”.I am trying to find things to improve
my web site!I suppose its ok to use a few of your ideas!!

タイトル
名前
Url
コメント