SqlException没有在Winforms中捕获 [英] SqlException not caught in Winforms

查看:146
本文介绍了SqlException没有在Winforms中捕获的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  try 
{
using(SqlConnection sourceCnx = new SqlConnection SOURCE_CONN_STRING))
{
sourceCnx.Open();
SqlCommand sysCmd = sourceCnx.CreateCommand();
sysCmd.CommandText =我的查询;
sysCmd.ExecuteNonQuery();
}
}
catch(异常)
{
//抛出一个消息框,通知出现错误
}

当用户不再连接到互联网时,我想显示一条消息。
但是当我调试我的程序没有互联网连接,程序崩溃与SqlException。 (catch块没有捕获异常)



我试图添加 catch(SqlException){// code} before catch(Exception),但它不起作用。
我仍​​然有一个异常,而不是catch块显示的消息。



我不知道该怎么办,因为如果我创建一个方法来测试互联网连接(尝试 ping google.com)然后返回true如果没关系,它将是一样的:我有一个例外,因为没有互联网连接。



任何想法?

解决方案

当引发CLR异常时,您已经设置了环境始终中断。如果你愿意,可以这样离开,按F5继续执行你的程序。或者你可以关闭它(默认情况下关闭):



转到调试菜单,选择异常并确保通用语言运行时异常




I got this code:

try
{
    using (SqlConnection sourceCnx = new SqlConnection(SOURCE_CONN_STRING))
    {
        sourceCnx.Open();
        SqlCommand sysCmd = sourceCnx.CreateCommand();
        sysCmd.CommandText = "My query";
        sysCmd.ExecuteNonQuery();
    }
}
catch (Exception)
{
    //Throwing a message box informing that there is an error
}

I want to display a message when the user is no longer connected to the internet. But when I debug my program without an internet connection, the program crashes with a SqlException. (The "catch" block does not catch the exception)

I tried to add catch (SqlException) { // code } before catch (Exception) but it doesn't work. I still have an exception instead of a message displayed by the catch block.

I don't know what to do because if I create a method to test the internet connection (try to ping google.com) then return true if it's ok, it will be the same : I got an exception because of no internet connection.

Any idea?

解决方案

You have set your environment to always break when a CLR exception is thrown. You can leave it like that if you wish and press F5 to carry on execution of your program. Or you can turn this off (it is switched off by default):

Go to Debug menu, select Exceptions and ensure Common Language Runtime Exceptions is not checked.

这篇关于SqlException没有在Winforms中捕获的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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