尝试/抓住没有抓住 [英] Try/Catch does not catch
问题描述
在下面的代码中,我有意地将@fooData错误地写入@ foo111Data,以检查try语句是否捕获我的异常。见下面的代码。但是try / catch语句没有在MessageBox中捕获并显示和异常,VS2010只是分解并突出显示错误代码行。
In following code I purposefully mistype "@fooData" to "@foo111Data" to check if the try statement is catching my exception. See below code. But the try/catch statement did not catch and display and exception in MessageBox, and VS2010 just break down and highlight the line of wrong code.
try
{
conn.Open();
cmd.Parameters.AddWithValue("@foo111Data", dataStrTb1.Text);
cmd.ExecuteNonQuery();
}
catch (SqlCeException ex)
{
MessageBox.Show(ex.ToString());
}
finally
{
conn.Close();
}
推荐答案
正在抛出一个不同类型的问题?我建议您更改 catch
,以便它仅捕获一般的异常
,看看它是否是另一种类型。
Perhaps an exception of a different type is being thrown? I would suggest that you change the catch
so that it just catches a general Exception
, and see if it's throwing another type.
在 catch
语句中放置一个断点 MessageBox.Show
行,然后您可以检查异常
。
Put a breakpoint in the catch
statement, on the MessageBox.Show
line, and then you can examine the Exception
.
这篇关于尝试/抓住没有抓住的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!