只有在尝试不捕获时才执行的代码

时间:2011-12-28 16:52:06

标签: c# try-catch

在按钮上单击我将信息保存到Sqlite数据库。我在try块中有command.ExecuteNonQuery()。我已经处理完所有事情只是找到catch块被捕获,但如果一切都通过,我希望其他代码执行,这将清除我的EditTexts的值并设置焦点。我尝试将该代码放在我的try块中的ExecuteNonQuery()之后,但它仍然在catch块之前执行,即使捕获到异常,因此我的edittexts的值在catch块甚至可以执行任何操作之前被清除。如果我完全在try / catch块之后添加代码,那就相同了。 catch块似乎是最后执行的事情,到那时值已被清除,catch块甚至无法正常执行。如何在清除catch块并且不抛出任何异常后将值设置为clear?

编辑:尝试把它放在最后一块但是同样的事情。 locals窗口显示partnumber.Text和partQty.text在到达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();
        }
    }

6 个答案:

答案 0 :(得分:7)

您可以尝试将它们放入finally块中。

try {/*execute code*/}
catch(System.Exception e){/*handle exceptions*/}
finally {/*clean up regardless if an exception was thrown or not*/}

答案 1 :(得分:2)

在“try”块之前放置bool并将其设置为值。如果您显示警报,请将bool设置为相反的值,然后根据该值继续。

bool EverythingIsFine = true;
try{
     //Your code
}
catch(Exception){
    if(Condition){
        EverythingIsFine = false;
        ShowRelatedAlerts();
    }
}
if(!EverythingIsFine){
    //DoMoreStuff
}

答案 2 :(得分:1)

如果抛出异常,try块内的执行会立即停止。因此,只有在没有引发异常的情况下才会执行try块的最后一行。

答案 3 :(得分:1)

看起来你在catch块中有类似的代码,我很确定这是正在执行的内容,而不是抛出错误的Sql语句之后的代码

在抛出异常的语句之后设置一个断点,你会发现它没有被击中。

答案 4 :(得分:0)

NotMyself's answer

提供更多背景信息
   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();
    }

答案 5 :(得分:-1)

使用try-catch-finally

    try
    {
       // Try something
    }
    catch (System.IO.IOException e)
    {
       // Handle exception
    }
    finally
    {
       // Some cleanup
    }