尝试Catch和Variables [英] Try Catch and Variables

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

问题描述




我正在尝试做一些非常简单的事情。在

类中创建一个方法,它将连接到数据库,拉回结果并且

将其返回给方法。不难。


所有我的数据库连接和执行的标准工作正常,但是,我想将

放入Try Catch语句,以便在出现问题时,它b / b
将错误输出到我的错误页面。


我怎样才能完成所有这些并在Try Catch中返回一个值


public int getUserIdByUsernamePassword(String username,String

userPassword)

{

try

{

databaseConnect.Open();


int userId;


cmdGetUserDetailsByUsernamePassword.Parameters [" ; @ username"]。值=

用户名;

cmdGetUserDetailsByUsernamePassword.Parameters [" @ userPassword"]。值

= userPassword;


userId =(int)cmdGetUserDetailsByUsernamePassword.ExecuteSc alar();


返回userId


}

catch

{

//错误页面调用。

}


}


该方法无法识别我有一个返回值的事实。


希望有人可以给我一个解释,我仍然可以获得

的好处一个Try Catch语句,同时仍然可以从方法中返回

值。


非常感谢

解决方案

Bungle写道:


public int getUserIdByUsernamePassword(String username,String
userPassword)
{
int userId -1;尝试
{/> dataConnect.Open();

cmdGetUserDetailsByUsernamePassword.Parameters [" @ username"]。值=
用户名;
cmdGetUserDetailsByUsernamePassword.Parameters [" @ userPassword"]。值
= userPassword;

userId =(int)cmdGetUserDetailsByUsernamePassword.ExecuteSc alar();

}
catch (例外情况){
//错误页面调用。
}
终于

{

返回userId

}
}




例如,如果我得到你的问题,这可以解决你所有的问题

吧。 />

每次处理完成后,每次执行finally语句

,或者尝试完美。


我将此用于数据库回滚或提交内容。一个非常有用的结构!


最诚挚的问候,


Martin





将一个return语句放入catch块或方法结尾。

你会让编译器开心。


-

Miha Markic [MVP C#] - RightHand .NET咨询&软件开发

miha at rthand com
www.rthand.com


" Bungle" < BU **** @ wizardbuy.com>在消息中写道

news:de ************************** @ posting.google.c om ...



我正在尝试做一些非常简单的事情。在
类中创建一个方法,该方法将连接到数据库,拉回结果并将其返回给方法。并不难。

所有我的数据库连接和执行标准工作正常,但是,我想将它放入Try Catch语句中,这样如果出现问题,它将会出错到我的错误页面。

如何做到这一切并在Try Catch中返回一个值

public int getUserIdByUsernamePassword(String username,String
userPassword)
尝试
{datConnect.Open();

int userId;

cmdGetUserDetailsByUsernamePassword.Parameters [" @ username"]。值=
用户名;
cmdGetUserDetailsByUsernamePassword.Parameters [" @ userPassword"]。值
= userPassword;

userId =( int)cmdGetUserDetailsByUsernamePassword.ExecuteSc alar();

返回userId

}
//
//错误页面调用。
}



该方法无法识别我返回值的事实。

希望有人可以给我一个解释我仍然可以获得Try Catch语句的好处,同时仍然可以从方法中返回
值。

非常感谢



感谢Martin的回复。


不幸的是,它不喜欢finally语句中的返回。


它给出错误控制不能离开最后一个子句的主体??


这些是我一直打的那种墙。


谢谢


***通过Developersdex发送 http://www.developersdex.com ***

不要只是参加USENET ......获得奖励!

Hi

I am trying to do something really simple. Create a method within a
class which will connect to the database, pull back a result and
return it out the method. Not hard.

All my database connections and executescalar work fine, but, I want
to put it into a Try Catch statement so that if a problem occurs, it
will error out to my error page.

How can I do all this and return a value within the Try Catch???

public int getUserIdByUsernamePassword(String username, String
userPassword)
{
try
{
databaseConnect.Open();

int userId;

cmdGetUserDetailsByUsernamePassword.Parameters["@username"].Value =
username;
cmdGetUserDetailsByUsernamePassword.Parameters["@userPassword"].Value
= userPassword;

userId = (int)cmdGetUserDetailsByUsernamePassword.ExecuteSc alar();

return userId

}
catch
{
// Error page call.
}

}

The method does not recognise the fact that I have a returned a value.

Hopefully someone can give me an explanation how I can still have the
benefits of a Try Catch statement while still being able to return a
value out of the method.

Thank you very much

解决方案

Bungle wrote:


public int getUserIdByUsernamePassword(String username, String
userPassword)
{ int userId -1; try
{
databaseConnect.Open();

cmdGetUserDetailsByUsernamePassword.Parameters["@username"].Value =
username;
cmdGetUserDetailsByUsernamePassword.Parameters["@userPassword"].Value
= userPassword;

userId = (int)cmdGetUserDetailsByUsernamePassword.ExecuteSc alar();

} catch(Exception ex) {
// Error page call.
} finally
{
return userId
}
}



This for example should solve all your problems, if I get your problem
right.

The finally statement is executed every time after the catch was
processed, or the try worked perfectly.

I use this for Database rollback or commit stuff. A very usefull construct!

Best Regards,

Martin


Hi,

Put a return statement into catch block or at the end of method.
You''ll make compiler happy.

--
Miha Markic [MVP C#] - RightHand .NET consulting & software development
miha at rthand com
www.rthand.com

"Bungle" <bu****@wizardbuy.com> wrote in message
news:de**************************@posting.google.c om...

Hi

I am trying to do something really simple. Create a method within a
class which will connect to the database, pull back a result and
return it out the method. Not hard.

All my database connections and executescalar work fine, but, I want
to put it into a Try Catch statement so that if a problem occurs, it
will error out to my error page.

How can I do all this and return a value within the Try Catch???

public int getUserIdByUsernamePassword(String username, String
userPassword)
{
try
{
databaseConnect.Open();

int userId;

cmdGetUserDetailsByUsernamePassword.Parameters["@username"].Value =
username;
cmdGetUserDetailsByUsernamePassword.Parameters["@userPassword"].Value
= userPassword;

userId = (int)cmdGetUserDetailsByUsernamePassword.ExecuteSc alar();

return userId

}
catch
{
// Error page call.
}

}

The method does not recognise the fact that I have a returned a value.

Hopefully someone can give me an explanation how I can still have the
benefits of a Try Catch statement while still being able to return a
value out of the method.

Thank you very much



Thanks for the reply Martin.

Unfortunately it does not like the return in the finally statement.

It gives the error "Control cannot leave the body of a finally clause"??

These were the kind of walls I kept hitting.

Thanks

*** Sent via Developersdex http://www.developersdex.com ***
Don''t just participate in USENET...get rewarded for it!


这篇关于尝试Catch和Variables的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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