只在Try未捕获时执行的代码
本文关键字:执行 代码 Try 只在 | 更新日期: 2023-09-27 18:18:53
在一个按钮上单击我保存信息到Sqlite数据库。我有命令。executenonquery()在一个尝试块。我已经处理了一切,只是发现如果捕获块被捕获,但如果一切都使它通过刚刚好,我想要其他代码执行,这将清除我的EditTexts的值并设置焦点。我尝试将该代码放在我的try块中的ExecuteNonQuery()之后,但即使捕获异常,它仍然在catch块之前执行,因此在catch块甚至可以做任何事情之前清除我的edittexts的值。同样的故事,如果我添加代码后,我的try/catch块完全。catch块似乎是执行的最后一个东西,到那时,值已经被清除,catch块甚至不能正常执行。如何将值设置为仅在清除catch块并且不抛出异常之后才清除?
编辑:试着把它放在一个finally块,但同样的事情。本地窗口显示两个部件号。短信和聚会。文本在到达catch块时是空白的。但是如果我取出清除这些字段的代码,那么它们在catch块中仍然有它们的值。是否有一些特殊的Sqlite异常可能会造成时间问题?
try
{
c.ExecuteNonQuery();
partnumber.Text = "";
partqty.Text = "";
partnumber.RequestFocus();
}
catch (SqliteException ex)
{
if (ex.ErrorCode.ToString() == "Constraint")
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle("Item Duplication");
builder.SetMessage("You have already counted this item. How would you like to proceed?");
builder.SetPositiveButton("Add to Previous", delegate
{
var newQty = Convert.ToInt32(test.currQuantity(partnumber.Text)) + Convert.ToInt32(partqty.Text);
var n = connection.CreateCommand();
connection.Open();
n.CommandText = "Update [Items] Set Quantity = '" + newQty.ToString() + "' Where ItemNmbr = '" + partnumber.Text + "'";
n.ExecuteNonQuery();
Toast.MakeText(this, "Quantity updated to: " + newQty.ToString(), ToastLength.Long)
.Show();
partnumber.Text = "";
partqty.Text = "";
partnumber.RequestFocus();
connection.Close();
return;
});
builder.SetNegativeButton("Override Previous", delegate
{
var n = connection.CreateCommand();
connection.Open();
n.CommandText = "Update [Items] Set Quantity = '" + partqty.Text + "' Where ItemNmbr = '" + partnumber.Text + "'";
n.ExecuteNonQuery();
Toast.MakeText(this, "Quantity updated to: " + test.currQuantity(partnumber.Text), ToastLength.Long)
.Show();
partnumber.Text = "";
partqty.Text = "";
partnumber.RequestFocus();
connection.Close();
return;
});
var dialog = builder.Create();
dialog.Show();
}
else
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.SetTitle("Error");
builder.SetMessage(ex.Message.ToString());
var dialog = builder.Create();
dialog.Show();
}
}
你可以试着把它们放到finally块中。
try {/*execute code*/}
catch(System.Exception e){/*handle exceptions*/}
finally {/*clean up regardless if an exception was thrown or not*/}
在"try"块之前放置一个bool
并将其设置为一个值。如果显示警报,请将bool值设置为相反的值,然后根据该值继续执行。
bool EverythingIsFine = true;
try{
//Your code
}
catch(Exception){
if(Condition){
EverythingIsFine = false;
ShowRelatedAlerts();
}
}
if(!EverythingIsFine){
//DoMoreStuff
}
如果抛出异常,try块内的执行将立即停止。因此,try块的最后一行只有在没有引发异常的情况下才会执行。
看起来你在catch块中有类似的代码,我很确定这是正在执行的代码,而不是抛出错误的Sql语句后的代码
在抛出异常的语句后面放一个断点,你会看到它没有被击中。
为NotMyself的答案提供更多上下文
try
{
c.ExecuteNonQuery();
}
catch (SqliteException ex)
{
// at this point the values in the partNumber textbox haven't been cleared out
showAlerts(ex);
}
finally
{
// clear the textbox after the code in the try block
// and the code in the catch block have executed (IF it executed)
partnumber.Text = "";
partqty.Text = "";
partnumber.RequestFocus();
}
使用try-catch-finally:
try
{
// Try something
}
catch (System.IO.IOException e)
{
// Handle exception
}
finally
{
// Some cleanup
}