异常而不是返回参数 [英] exceptions instead of return parameters

查看:53
本文介绍了异常而不是返回参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在其他一个主题中提到过,使用返回值

从方法指示方法的成功/失败应该是

替换为抛出异常。这意味着代码如下:


List l = object.Find(...);

if(l == null)... //没有回复


将替换为:


尝试{

列表l = object.Find(.. 。);

....

}

catch(NotFoundException ex)

{

}

catch(例外情况)

{

}


这是首选方法?这背景是什么?

dan

In one of the other threads it was mentioned that using return values
from methods to indicated success/failure of the method should be
replaced with throwing exceptions. Which would mean code like:

List l = object.Find(...);
if (l == null) ... //nothing returned

would be replaced with:

try{
List l = object.Find(...);
....
}
catch (NotFoundException ex)
{
}
catch (Exception ex)
{
}

Is this the preferred method? And what is the background on this?
dan

推荐答案

我的opion最好有一个函数返回一个bool for success

或faliure比抛出异常。例外情况是资源干预

所以尽可能。尝试这样的事情


public bool VerifyInitialLogin()

{

this.emailParamater.Value = this.emailAdress;

this.passwordParamater.Value = this.password1;


//选中要查看密码1和电子邮件

if(String .IsNullOrEmpty(this.password1)||

String.IsNullOrEmpty(this.emailAdress)){

返回false;

}


//从DB获取结果
尝试{

this.connection.Open();

this.loginDataReader =

this.loginInfoSelectcommand.ExecuteReader();


if(this.loginDataReader.HasRows){

this.clientID = this.loginDataReader.GetInt16(0);

this.password2 = this.loginDataReader.GetString(3);

this.firstName = this.loginDataReader。 GetString(4);

this.sureName = this.loginDataReader.GetString(5);

this.companyName =

this.loginDataReader。 GetString(6);

thi s.isFirstTime =

this.loginDataReader.GetBoolean(7);

if(!IsFirstTime){

this.reminderQuestion =

this.loginDataReader.GetString(8);

this.reminderQuestionAnswear =

this.loginDataReader.GetString(9);

}

}其他{

返回false;


}

} catch(MySqlException myEX ){

抛出myEX;

}终于{

this.loginDataReader.Close();

这个。 connection.Close();

}


返回true;

}


如果有一些DB错误,如果有一些逻辑错误,那么我只会抛出异常。

It is in my opion better to have a function return a bool for success
or faliure than throwing an exception. Exceptions are resource intesive
so where possible. try something like this

public bool VerifyInitialLogin ()
{
this.emailParamater.Value = this.emailAdress;
this.passwordParamater.Value = this.password1;

//checked To see there is a password1 and email
if (String.IsNullOrEmpty(this.password1) ||
String.IsNullOrEmpty(this.emailAdress)) {
return false;
}

//Get the results back from DB
try {
this.connection.Open();
this.loginDataReader =
this.loginInfoSelectcommand.ExecuteReader();

if (this.loginDataReader.HasRows) {
this.clientID = this.loginDataReader.GetInt16(0);
this.password2 = this.loginDataReader.GetString(3);
this.firstName = this.loginDataReader.GetString(4);
this.sureName = this.loginDataReader.GetString(5);
this.companyName =
this.loginDataReader.GetString(6);
this.isFirstTime =
this.loginDataReader.GetBoolean(7);
if (!IsFirstTime) {
this.reminderQuestion =
this.loginDataReader.GetString(8);
this.reminderQuestionAnswear =
this.loginDataReader.GetString(9);
}
} else {
return false;

}
} catch (MySqlException myEX) {
throw myEX;
} finally {
this.loginDataReader.Close();
this.connection.Close();
}

return true;
}

I only throw an exception if there is some DB error not if there is
some logic error.


抛出自定义异常是一个好习惯,是.NET的构建方式。但是

并不意味着,你不会返回null来表示某些事情

失败了......这就是我的看法..


抛出异常会给出

函数中发生的内容的详细错误消息,如果你选择放弃,如果没有,你可以只返回null

表示你​​在方法中失败了,返回null

VJ

" Dan Holmes" <哒******* @ bigfoot.com>在消息中写道

新闻:eR ************** @ TK2MSFTNGP04.phx.gbl ...
Throwing custom exceptions are a good habit and is how .NET is built. But
that does not mean, you don''t ever return null to indicate something
failed.. Here is how I see it..

Throwing exceptions gives a detail error message of what happened in your
function, if you choose to give that out, if not you can just return null to
indicate you failed in the method by returning null

VJ
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:eR**************@TK2MSFTNGP04.phx.gbl...
其中一个提到的线程使用
方法的返回值表示方法的成功/失败应该用抛出异常来代替。这意味着代码如下:

List l = object.Find(...);
if(l == null)... //没有返回

尝试{
List l = object.Find(...);
...
}
catch (NotFoundException ex)
{
}
catch(例外情况)
{
}

这是首选方法吗?这背景是什么?

dan
In one of the other threads it was mentioned that using return values from
methods to indicated success/failure of the method should be replaced with
throwing exceptions. Which would mean code like:

List l = object.Find(...);
if (l == null) ... //nothing returned

would be replaced with:

try{
List l = object.Find(...);
...
}
catch (NotFoundException ex)
{
}
catch (Exception ex)
{
}

Is this the preferred method? And what is the background on this?
dan



Dan,我还没读过你的另一个帖子请参考,但我或许可以用b $ b来帮助你解决一些问题...


在我看来,我不认为你的Find方法应该抛出一个

NotFoundException。我知道失败的定义是主观的,但在你的例子中,我不认为Find方法找不到任何东西;我认为

*成功找不到*。这可能听起来像是分裂的头发,

但我认为这说明了一个关键的区别。


看,我相信应该使用例外情况当一个方法出于某种原因无法执行其基本功能时。所以,如果Find无法开始

,因为数据库连接不可用或指定的目录路径

不存在,那么异常应该使用。但是如果搜索成功地完成了

,但实际上没有发现任何东西,那么该方法确实没有失败。从某种意义上说,搜索失败,但搜索*方法*没有
失败。


找不到搜索结果的条件是一个正常的结果

a查找方法。换句话说,这不是一个特殊的结果。现在,如果

搜索甚至无法启动,那么现在*这是一个非常特殊的情况,并且应该是一个例子。同样,我的经验法则是限制事件的例外情况,即b $ b阻止方法执行其功能,而不是传达正常结果

(即使结果为负) )。


- Mitchell S. Honnert

" Dan Holmes" <哒******* @ bigfoot.com>在消息中写道

新闻:eR ************** @ TK2MSFTNGP04.phx.gbl ...
Dan, I haven''t read the other thread to which you refer, but I might be able
to help a bit with your question...

In my opinion, I don''t think your Find method should throw a
NotFoundException. I know the definition of "failure" is subjective, but in
your example, I don''t think the Find method failed to find anything; I think
it *successfully found nothing*. This may sound like it''s splitting hairs,
but I do think it illustrates a key distinction.

See, I believe that exceptions should be used when a method fails to perform
its basic function for some reason. So, if the Find couldn''t even begin
because a database connection wasn''t available or a specified directory path
didn''t exist, then an exception should be used. But if the search completed
successfully, but it just so happened that nothing was found, the method did
not fail. In a sense, the search failed, but the search *method* didn''t
fail.

The condition of no results being found for a search is a normal outcome of
a Find method. In other words, it''s not an exceptional result. Now, if the
search can''t even start, now *that''s* an exceptional situation and deserves
an exeption. Again, my rule of thumb is to limit exceptions to events that
prevent a method from performing its function, not to convey a normal result
(even if that result is negative).

- Mitchell S. Honnert
"Dan Holmes" <da*******@bigfoot.com> wrote in message
news:eR**************@TK2MSFTNGP04.phx.gbl...
其中一个提到的线程使用
方法的返回值表示方法的成功/失败应该用抛出异常来代替。这意味着代码如下:

List l = object.Find(...);
if(l == null)... //没有返回

尝试{
List l = object.Find(...);
...
}
catch (NotFoundException ex)
{
}
catch(例外情况)
{
}

这是首选方法吗?背景是什么?

dan
In one of the other threads it was mentioned that using return values from
methods to indicated success/failure of the method should be replaced with
throwing exceptions. Which would mean code like:

List l = object.Find(...);
if (l == null) ... //nothing returned

would be replaced with:

try{
List l = object.Find(...);
...
}
catch (NotFoundException ex)
{
}
catch (Exception ex)
{
}

Is this the preferred method? And what is the background on this?
dan



这篇关于异常而不是返回参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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