坚持返回值 [英] stuck with return value

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

问题描述




我有一个将数据添加到数据库的函数,如果

数据库已更新,我想返回true,如果有,则返回false是一个问题,所以我使用了一个

尝试...抓住块...


我的问题是我可以返回真的确定是常规成功,但在哪里做

我把回报称为假;语句ID进程失败 - 我得到一个编译

错误说无法访问的代码与我尝试的任何东西...


代码片段:


public bool添加()

{

试试

{

//做东西

返回true;

}

catch(例外情况)

{

抛出新的异常(无法更新数据库+ ex.Message)

}

}


如果我放在投掷之前返回假,然后抛出是无法到达的,如果我把它放在那之后那么返回是无法到达的......这是什么方法做什么?/ b
这个? ?


提前致谢

Hi,

I have a function that adds data to a database and I want to return true if
the database was updated and false if there was a problem, so I am using a
try... catch block...

My problem is that I can return true ok is the routine succeeds but where do
I put the return false; statement id the process fails - I get a compile
error say unreachable code with whatever i try...

Code snippet:

public bool Add()
{
try
{
// Do stuff
return true;
}
catch (Exception ex)
{
throw new Exception ("Unable to update database " + ex.Message)
}
}

If i put return false before the throw then the throw is unreachable, if I
put it after then the return is unreachable.... what is the proper way to do
this??

Thanks in advance

推荐答案

> public bool Add()
> public bool Add()
{
尝试
// 返回true;
}
catch(例外情况
{
抛出新的异常(无法更新数据库+ ex.Message)
}
}
{
try
{
// Do stuff
return true;
}
catch (Exception ex)
{
throw new Exception ("Unable to update database " + ex.Message)
}
}



您必须决定是否返回false或抛出异常。你不能这样做两个孩子。在你的代码中,高级方法必须捕获异常

(因为throw)因此不会对返回值感兴趣。


void HighLevelMethod()

{

试试

{

bool res = Add();

}

catch

{

//如果你在Add()中抛出异常那么执行就会

//继续这里没有从Add()返回的值

}

}


Wiktor Zychla


谢谢,


有意义

" Wiktor Zychla"写道:
Thanks,

that makes sense
"Wiktor Zychla" wrote:
public bool Add()
{
尝试
//

返回true;
}
catch(Exception ex)
{
抛出新的异常(无法更新数据库+ ex.Message)
}
}
public bool Add()
{
try
{
// Do stuff
return true;
}
catch (Exception ex)
{
throw new Exception ("Unable to update database " + ex.Message)
}
}



你必须决定是否返回false或抛出异常。你做不到两个。在你的代码中,高级方法必须捕获异常
(因为throw)因此不会对返回值感兴趣。

void HighLevelMethod()
{
尝试
{
bool res = Add();
}
{
//如果你在Add中抛出异常( )然后执行将
//继续在这里没有返回Add()
}


Wiktor Zychla



you have to decide if you return false or throw an exception. you cannot do
both. in your code the high-level method will have to catch the exception
(because of throw) and thus will not be interested in returned value.

void HighLevelMethod()
{
try
{
bool res = Add();
}
catch
{
// if you throw the exception inside Add() then the execution will
// continue here with no value returned from Add()
}
}

Wiktor Zychla



取决于您在异常处理方面的工作......


如果您想将异常传递给更高级别并处理那里的

错误(通常是OOP的公认经验法则),然后添加

void而不是bool。如果发生错误,那么

更高代码中的try / catch块将捕获并处理它。


你需要查看代码和如果

添加失败,请确定你想要发生什么...

例如,如果它对你正在做的事情没有任何影响,那就不是a / $
主要问题,然后将问题记录为警告(包装代码,因为你已完成
,但记录它而不是重新抛出错误)并继续。

如果这是一个重大错误并且你的应用程序无法在没有它的情况下继续运行,那么你应该把错误赶上去,如果可能的话记录下来(无论是磁盘,还是

到数据库或向用户发送消息),并关闭应用程序。


另外需要考虑的是哪个会在这里起作用是否有

任何需要关闭的东西在离开功能之前。我怀疑

你将打开一个数据库连接,你想要关闭...

在你的Add()中这样做:


SqlConnection myConnection = new SqlConnection();

试试

{

//打开连接和在这里做你的东西

}

catch(exception ex)

{

throw(ex);

}

最后

{

if(myConnection.State == ConnectionState.Open)

{

myConnection.Close();

}

}


最后,请记住您可能想要捕获某些类型的异常...

例如,如果连接无法建立(SqlException是

抛出),您可以做一些不同的事情在你的异常处理中,如果引用了

a null对象...

" hplloyd" <马力***** @ discussions.microsoft.com>在消息中写道

新闻:49 ********************************** @ microsof t.com ...
Depends on what you''re doing regarding exception handling...

If you want to pass your exceptions up to the higher level and handle the
errors there (generally an accepted rule of thumb of OOP), then make Add
void and not bool. If an error occurs, then the try/catch blocks in the
higher code will catch and deal with it.

You need to look at the code and decide what you want to happen should the
Add fail...
For example, if it makes no difference to what you''re doing and it''s not a
major problem, then log the problem as a warning (wrap the code as you have
done, but log it rather than rethrowing the error) and carry on.
If it is a major error and your application cannot proceed without it, then
you should catch the error higher up, log it if possible (whether to disk,
to database or message to the user), and shut the application down.

Another thing to consider which will play a part here is whether there is
anything that needs "shutting" down before leaving the function. I suspect
you''ll be opening a database connection, which you''ll want to close...
Do this like this inside your Add():

SqlConnection myConnection = new SqlConnection();
try
{
// open connection and do your stuff here
}
catch ( Exception ex )
{
throw ( ex );
}
finally
{
if ( myConnection.State == ConnectionState.Open )
{
myConnection.Close();
}
}

Finally, remember that you may want to catch certain kinds of exceptions...
For example, if the connection fails to be established (SqlException is
thrown), you could do something different in your exception handling than if
a null object was referenced...
"hplloyd" <hp*****@discussions.microsoft.com> wrote in message
news:49**********************************@microsof t.com...


我有一个将数据添加到数据库的功能,我想返回true
如果
数据库已更新,如果出现问题则为false,所以我正在使用
尝试... catch块...

我的问题是我可以返回真的确定例程成功但是
做什么
我把回报弄错了;语句ID进程失败 - 我得到一个编译
错误说无法访问的代码与我尝试的任何东西...

代码片段:

public bool Add()
{
尝试
// 返回true;
}
catch(Exception ex)
{}

如果我在抛出之前将return返回false,则抛出无法访问,如果我把它放在那之后就无法返回......什么是正确的方式来做这个?

提前致谢
Hi,

I have a function that adds data to a database and I want to return true
if
the database was updated and false if there was a problem, so I am using a
try... catch block...

My problem is that I can return true ok is the routine succeeds but where
do
I put the return false; statement id the process fails - I get a compile
error say unreachable code with whatever i try...

Code snippet:

public bool Add()
{
try
{
// Do stuff
return true;
}
catch (Exception ex)
{
throw new Exception ("Unable to update database " + ex.Message)
}
}

If i put return false before the throw then the throw is unreachable, if I
put it after then the return is unreachable.... what is the proper way to
do
this??

Thanks in advance



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

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