为什么总走到Catch语句呢?(c#)
来源:百度知道 编辑:UC知道 时间:2024/06/13 22:48:51
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<script language="C#" runat="server">
void page_Load()
{
string strConnection = "server=mySQLServer;";
strConnection += "database=zhangtianxing;uid=sa;password=cjj";
SqlConnection sqlConnection1 = new SqlConnection(strConnection);
try
{
sqlConnection1.Open();
Response.Write("happy");
}
catch
{
Response.Write("数据库连接错误");
}
}
</script>
uid 和 password 没有错啊!
<%@ Import Namespace="System.Data.SqlClient"%>
<script language="C#" runat="server">
void page_Load()
{
string strConnection = "server=mySQLServer;";
strConnection += "database=zhangtianxing;uid=sa;password=cjj";
SqlConnection sqlConnection1 = new SqlConnection(strConnection);
try
{
sqlConnection1.Open();
Response.Write("happy");
}
catch
{
Response.Write("数据库连接错误");
}
}
</script>
uid 和 password 没有错啊!
因为连接数据库有问题,在执行try快里的语句时出现异常,所以就会执行catch块里的语句。
catch后面没有带异常,你换成下面这样看看是出了什么异常.
然后再针对问题下药
<%@ Import Namespace="System.Data"%>
<%@ Import Namespace="System.Data.SqlClient"%>
<script language="C#" runat="server">
void page_Load()
{
string strConnection = "server=mySQLServer;";
strConnection += "database=zhangtianxing;uid=sa;password=cjj";
SqlConnection sqlConnection1 = new SqlConnection(strConnection);
try
{
sqlConnection1.Open();
Response.Write("happy");
}
catch (Exception ex)
{
Response.Write("数据库连接错误" + ex.Message);
}
}
</script>
这是C#的异常处理机制。
当在执行方法中的throw语句或者溢出时等,就会发生异常。
发生异常时将控制转入catch语句。
为什么总走到Catch语句呢?(c#)
为什么一定要在程序里使用try ... catch语句?
for weeks we have been trying to catch the bank robber.为什么catch不用过去式呢?
eclipse中如何快速的写出try catch语句
C#中try-catch语句块中数据库连接的释放
java:try catch语句中的值与外部共享问题
JAVA程序中怎么样运用try catch输入语句,麻烦高手
C++编程中哪些情况需要用try/catch语句块?
写了throws语句之后,一定要用try-catch捕获吗
C#中try catch语句在debug和release版中都能使用吗?