如何处理以下代码的异常 [英] How to handle exception of following code

查看:84
本文介绍了如何处理以下代码的异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码

I have following code

try
            {
                SqlCommand OpenCat = new SqlCommand(SQL, MainClass.Conn);
                SqlDataReader readCat = OpenCat.ExecuteReader();
                if (readCat.Read())
                {
                    cmbPettyCashCode.Text = MainClass.SetDefo(readCat.GetValue(0).ToString(), "");
                    txtCatName.Text = MainClass.SetDefo(readCat.GetValue(1).ToString(), "");
                    readCat.Dispose();
                }
                else
                {
                    readCat.Dispose();
                }
               
            }
            catch (Exception error)
            {
                
                cmbPettyCashCode.Text = "";
                MessageBox.Show(error.Message.ToString(), "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
            }
            finally {  }



我看不到OpenCat和readCat在捕获点如何处理这个



谢谢


I Can't See "OpenCat" and "readCat" at the catch point how to handle this

thanks

推荐答案

最佳实践正在使用使用BLOCK如下所示,它将在发生异常时处理连接/命令/阅读器

best practice is using "USING BLOCK" as below, it will dispose the connection/command/ reader even when exception occurred
using(SqlConnection connection = ...)
{
    connection.Open();
    ...
    using(SqlCommand command = ...)
    {
        using(SqlDataReader reader = command.ExecuteReader())
        {
            ... do your stuff ...
        } // reader is closed/disposed here
    } // command is closed/disposed here
} // connection is closed/disposed here





参考理解C#中的使用语句 [ ^ ]

SQLDataReader dispose? [ ^ ]


如果要在catch和/或finally块中访问它们,则需要在try块之外声明变量。



由于@DamithSL已经回答,请使用使用语句确保您的连接将在最后处理,是否发生异常。



您仍然可以使用中编写 try / catch 块c $ c>阻止,这样你的 catch 块可以专注于错误处理:将异常详细信息写入日志文件并向用户显示错误消息。
You will need to declare your variables outside of the try block if you want to access them in the catch and/or finally block.

As @DamithSL already answered, use using statement to make sure your connection will be disposed at the end, whether an exception occurs or not.

You can still write your try/catch blocks within the using block and this way your catch block can focus on error handling: write exception details to log file and display error message to user.


SqlCommand OpenCat = new SqlCommand(SQL, MainClass.Conn);
SqlDataReader readCat = new SqlDataReader();
try
{
readCat = OpenCat.ExecuteReader();
if (readCat.Read())
{
cmbPettyCashCode.Text = MainClass.SetDefo(readCat.GetValue(0).ToString(), "");
txtCatName.Text = MainClass.SetDefo(readCat.GetValue(1).ToString(), "");
readCat.Dispose();
}
else
{
readCat.Dispose();
} 
}
catch (Exception error)
{
//access your variables here, as they are now global 
cmbPettyCashCode.Text = "";
MessageBox.Show(error.Message.ToString(), "Message", MessageBoxButtons.OK, MessageBoxIcon.Exclamation);
}
finally { }


这篇关于如何处理以下代码的异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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