且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

这个try-catch块有什么问题?

更新时间:2023-11-29 14:43:04

为什么不简单地在这部分代码后面设置一个断点,看看它是什么是?


testConnection.ToString())

" HillBilly" < so ****** @ somewhere.comschreef in bericht

news:ur ************** @ TK2MSFTNGP03.phx.gbl ...
Why not simple set a breakpoint behind this part of code and see what it is?

testConnection.ToString())
"HillBilly" <so******@somewhere.comschreef in bericht
news:ur**************@TK2MSFTNGP03.phx.gbl...

这让我大吃一惊。我正在使用会员资格并试图确定数据库是否在线是否为
。 GetConnectionString()方法按预期返回

连接字符串,但在try块中使用时则不返回。


我有Open()和Close()使用

Response.Redirect进行测试时注释掉的方法。

如图所示,try块中的代码将 - 执行 - 但是

catch块 - 总是执行 - 数据库是否在线。


WTF ???


protected void LoginButton_Click(object sender,EventArgs e)

{

//重定向证明此测试有效

//Response.Redirect(ResolveUrl( "〜/ homepage.aspx?LoginButton_Click"));


SqlConnection testConnection = new

SqlConnection(GetConnectionString(" MyConnectionStr ing)));

if(!String.IsNullOrEmpty(testConnection.ToString()))

{

试试

{

//testConnection.Open();

//testConnection.Close();


Response.Redirect(ResolveUrl("〜/ homepage.aspx?MyConnectionString-Found"));

}

catch(exception ex)

{


Response.Redirect(ResolveUrl("〜/ homepage.aspx?LoginButton_Click- Caught-Exception));

}

}

}
This is freaking me out. I''m using Membership and trying to determine if
the database is online. The GetConnectionString( ) method returns a
connection string as expected but not when used in the try block.

I have the Open() and Close() methods commented out while using the
Response.Redirect to test.
The code in the try block as shown will --never execute-- but the code in
the catch block --always executes-- whether the database is online or not.

WTF???

protected void LoginButton_Click(object sender, EventArgs e)
{
// Redirect proves this test works
//Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Click"));

SqlConnection testConnection = new
SqlConnection(GetConnectionString("MyConnectionStr ing"));
if (!String.IsNullOrEmpty(testConnection.ToString()))
{
try
{
//testConnection.Open();
//testConnection.Close();

Response.Redirect(ResolveUrl("~/homepage.aspx?MyConnectionString-Found"));
}
catch (Exception ex)
{

Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Click-Caught-Exception"));
}
}
}


7月31日,11:51 * pm,HillBilly < someb ... @ somewhere.comwrote:
On Jul 31, 11:51*pm, "HillBilly" <someb...@somewhere.comwrote:

这让我大吃一惊。我正在使用会员资格并试图确定

数据库是否在线。 GetConnectionString()方法按预期返回一个连接

字符串,但在try块中使用时则不返回。


我有Open()和Close()使用

Response.Redirect进行测试时注释掉的方法。

如图所示,try块中的代码将 - 执行 - 但是

catch块 - 总是执行 - 无论数据库是否在线..


WTF ???


protected void LoginButton_Click(object sender,EventArgs e)

{

* * //重定向证明此测试有效

* * //响应.Redirect(ResolveUrl("〜/ homepage.aspx?LoginButton_Click"));


* * SqlConnection testConnection = new

SqlConnection(GetConnectionString(" MyConnectionStr ing));

* * if(!String.IsNullOrEmpty(testConnection.ToString()))

* * {

* * * *尝试

* * * * {

* * * * * * //testConnection.Open();

* * * * * * //testConnection.Close();

* * * * * * Response.Redirect(ResolveUrl("〜/ homepage.aspx?MyConnectionString-Found"));

* * * *}

* * * * catch(例外情况)

* * * * {

* * * * * * Response.Redirect(ResolveUrl("〜/ homepage.aspx?LoginButton_Click-Caught-Exce * ption"));

* * * *}

* *}


} - 隐藏引用的文字 -


- 显示引用的文字 -
This is freaking me out. I''m using Membership and trying to determine if the
database is online. The GetConnectionString( ) method returns a connection
string as expected but not when used in the try block.

I have the Open() and Close() methods commented out while using the
Response.Redirect to test.
The code in the try block as shown will --never execute-- but the code in
the catch block --always executes-- whether the database is online or not..

WTF???

protected void LoginButton_Click(object sender, EventArgs e)
{
* * // Redirect proves this test works
* * //Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Click"));

* * SqlConnection testConnection = new
SqlConnection(GetConnectionString("MyConnectionStr ing"));
* * if (!String.IsNullOrEmpty(testConnection.ToString()))
* * {
* * * * try
* * * * {
* * * * * * //testConnection.Open();
* * * * * * //testConnection.Close();
* * * * * * Response.Redirect(ResolveUrl("~/homepage.aspx?MyConnectionString-Found"));
* * * * }
* * * * catch (Exception ex)
* * * * {
* * * * * * Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Click-Caught-Exce*ption"));
* * * * }
* * }

}- Hide quoted text -

- Show quoted text -



就在你的if语句之前,应用程序抛出一个消息框,其中包含

字符串的内容:

MessageBox.Show(testConnection.ToString() );

Right before your if statement have the app throw a messagebox with
the contents of the string:
MessageBox.Show(testConnection.ToString());


我可以在try语句中设置断点--except--。我没看到任何异常 - 除了 - 一个错误列表声明:变量

''ex''被声明但从未使用注意如果我更改(Exception ex)到

(例外e)我在同一范围内对已经使用的变量

有不同的错误。


我设置了断点,我可以读取Call Stack和Locals窗口。我ñ
真的不知道该找什么,这表明为什么try

块中的代码不会执行。我只能假设关于ex的愚蠢错误

对我来说没有意义可能是问题。


我可以看到连接字符串并知道它有效和功能。可能

不是***做法,但我不知道任何其他方式来确定

数据库何时可能处于脱机状态,所以我试图强迫例外我很容易通过使用

testConnection.Open()方法在数据库可能脱机的情况下捕获
,如果数据库是

在线,但离线时应该被catch块捕获。


这对我来说太奇怪了,即使这段代码也不允许重定向在

尝试阻止执行。 click事件继续执行catch中的代码

LoginButton_Click-Caught-Exception ...


protected void LoginButton_Click(object sender,EventArgs e)

{

//重定向证明此测试有效

//Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Clicked")) ;


if(1 == 1)

{

试试

{

Response.Redirect(ResolveUrl("〜/ homepage.aspx?LoginButton_Click-Try-Block"));


}

catch (例外情况)

{

Response.Redirect(ResolveUrl("〜/ homepage.aspx?LoginButton_Click-Caught-Exception"));

}

}


}

" rhaazy" < rh **** @ gmail.com写信息

新闻:0c ************************** ******** @ s50g2000 hsb.googlegroups.com ...

7月31日晚上11:51,HillBilly < someb ... @ somewhere.comwrote:
I can set breakpoints all over the place --except-- on the try statement. I
don''t see anything unusual --except-- an the Error List states: The variable
''ex'' is declared but never used noting if I change (Exception ex) to
(Exception e) I get a different error about the variable already being used
in the same scope.

I set breakpoints and I can read the Call Stack and the Locals window. I
really don''t know what to look for that indicates why the code in the try
block will not execute. I can only assume the stupid error about ex which
makes no sense to me may be the problem.

I can see the connection string and know its valid and functional. It may
not be a best practice but I don''t know any other way to determine when the
database may be offline so I''m trying to force an exception I could easily
catch if and when the database may be offline by using the
testConnection.Open() method which will open and close if the database is
online but should be caught by the catch block when offline.

This is so strange to me not even this code will allow the redirect in the
try block to execute. The click event keeps executing the code in the catch
LoginButton_Click-Caught-Exception...

protected void LoginButton_Click(object sender, EventArgs e)
{
// Redirect proves this test works
//Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Clicked"));

if( 1 == 1 )
{
try
{
Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Click-Try-Block"));

}
catch (Exception ex)
{
Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Click-Caught-Exception"));
}
}

}
"rhaazy" <rh****@gmail.comwrote in message
news:0c**********************************@s50g2000 hsb.googlegroups.com...
On Jul 31, 11:51 pm, "HillBilly" <someb...@somewhere.comwrote:

这让我大吃一惊。我正在使用会员资格并试图确定

数据库是否在线。 GetConnectionString()方法按预期返回一个连接

字符串,但在try块中使用时则不返回。


我有Open()和Close()使用

Response.Redirect进行测试时注释掉的方法。

如图所示,try块中的代码将 - 执行 - 但是

catch块 - 总是执行 - 数据库是否在线。


WTF ???


protected void LoginButton_Click(object sender,EventArgs e)

{

//重定向证明此测试有效

//Response.Redirect(ResolveUrl( "〜/ homepage.aspx?LoginButton_Click"));


SqlConnection testConnection = new

SqlConnection(GetConnectionString(" MyConnectionStr ing)));

if(!String.IsNullOrEmpty(testConnection.ToString()))

{

试试

{

//testConnection.Open();

//testConnection.Close();

Response.R edirect(ResolveUrl(" ~~ homepage.aspx?MyConnectionString-Found"));

}

catch(Exception ex)

{

Response.Redirect(ResolveUrl("〜/ homepage.aspx?LoginButton_Click-Caught-Exce * ption"));

}

}


} - 隐藏引用的文字 -


- 显示引用的文字 -
This is freaking me out. I''m using Membership and trying to determine if
the
database is online. The GetConnectionString( ) method returns a connection
string as expected but not when used in the try block.

I have the Open() and Close() methods commented out while using the
Response.Redirect to test.
The code in the try block as shown will --never execute-- but the code in
the catch block --always executes-- whether the database is online or not.

WTF???

protected void LoginButton_Click(object sender, EventArgs e)
{
// Redirect proves this test works
//Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Click"));

SqlConnection testConnection = new
SqlConnection(GetConnectionString("MyConnectionStr ing"));
if (!String.IsNullOrEmpty(testConnection.ToString()))
{
try
{
//testConnection.Open();
//testConnection.Close();
Response.Redirect(ResolveUrl("~/homepage.aspx?MyConnectionString-Found"));
}
catch (Exception ex)
{
Response.Redirect(ResolveUrl("~/homepage.aspx?LoginButton_Click-Caught-Exce*ption"));
}
}

}- Hide quoted text -

- Show quoted text -



在if语句之前,应用程序抛出一个消息框,其中包含字符串的内容:

MessageBox.Show(testConnection.ToString());

Right before your if statement have the app throw a messagebox with
the contents of the string:
MessageBox.Show(testConnection.ToString());