尝试抓住问题 [英] Problems with try...catch

查看:68
本文介绍了尝试抓住问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问候,


我在C#中遇到问题。我正在使用ADO.NET,但这似乎是次要的

问题。


以下代码:


SqlConnection conn;

试试

{

conn.Open();

// ...

}

catch(异常错误)

{

// ...

}

终于

{

conn.Close();

}


不会编译。编译器抱怨conn被使用(在最终的

块内)而没有被初始化(或者那种效果)。所以我在一些书中查看了一些例子,他们在try块之前移动了conn.Open()




好​​吧,编译好了。但是,如果连接不能打开,会发生什么?那个错误不会被抓住。


我错过了什么吗?


谢谢。


-

Jonathan Wood

SoftCircuits编程
http://www.softcircuits.com

Greetings,

I''m having an issue in C#. I''m using ADO.NET but that seems secondary to the
issue.

The following code:

SqlConnection conn;
try
{
conn.Open();
// ...
}
catch (Exception err)
{
// ...
}
finally
{
conn.Close();
}

Won''t compile. The compiler complains that conn is used (within the finally
block) without having been initialized (or something to that effect). So I
looked at some examples in some books I have and they move the conn.Open()
prior to the try block.

Well, that compiles okay. But then what happens if the connection cannot be
opened? That error would not be caught.

Am I missing anything?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com

推荐答案

我认为你剪掉了这个问题...

我认为你最初有:


SqlConnection conn;

尝试{

conn = new SqlConnection(cs) ;

conn.Open();

...

}终于{

conn.Close() ;

}


问题是,如果第一行错误然后conn将没有初始化

。您可以通过将初始化程序

移动到变量声明行来解决这个问题,但更好的选择是使用

因为这也管理处理(包括关闭)因此:


使用(SqlConnection conn = new SqlConnection(cs)){

conn.Open();

。 ..

conn.Close();

}


如需完整说明,请查看使用和处理。


Marc

I think you snipped away the problem...
I think you originally had:

SqlConnection conn;
try {
conn = new SqlConnection(cs);
conn.Open();
...
} finally {
conn.Close();
}

The problem is that if the first line errors then conn will not have
been initialised. You could get around this by moving the initialiser
into the variable declaration line, but a better option is "using"
since this also manages disposal (which inclused closure), hence:

using(SqlConnection conn = new SqlConnection(cs)) {
conn.Open();
...
conn.Close();
}

For a full explanation, look up "using" and "Dispose".

Marc


这将解决您的问题:


SqlConnection conn = null;

尝试

{

}

catch(Exception ex )

{

}

终于

{

conn.Close() ;

}


但你真的应该使用使用这些类型的关键字

东西:


使用(SqlConnection conn = new SqlConnection(...))

{< br $>
// ...

}


" Jonathan Wood" < jw *** @ softcircuits.com写信息

news:u5 ************** @ TK2MSFTNGP02.phx.gbl ...
This will solve your problem:

SqlConnection conn = null;
try
{
}
catch (Exception ex)
{
}
finally
{
conn.Close();
}

but you really should be using the "using" keyword for these kinds of
things:

using (SqlConnection conn = new SqlConnection(...))
{
//...
}

"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:u5**************@TK2MSFTNGP02.phx.gbl...

问候,


我在C#中遇到问题。我正在使用ADO.NET,但这似乎是次要的

问题。


以下代码:


SqlConnection conn;

试试

{

conn.Open();

// ...

}

catch(异常错误)

{

// ...

}

终于

{

conn.Close();

}


不会编译。编译器抱怨conn被使用(在

finally块内)而没有被初始化(或某些东西到那个

效果)。所以我查看了一些书中的一些例子,他们在try块之前移动了

conn.Open()。


嗯,编译好的。但是如果连接不能打开会发生什么呢?那个错误不会被抓住。


我错过了什么吗?


谢谢。


-

Jonathan Wood

SoftCircuits编程
http://www.softcircuits.com

Greetings,

I''m having an issue in C#. I''m using ADO.NET but that seems secondary to
the issue.

The following code:

SqlConnection conn;
try
{
conn.Open();
// ...
}
catch (Exception err)
{
// ...
}
finally
{
conn.Close();
}

Won''t compile. The compiler complains that conn is used (within the
finally block) without having been initialized (or something to that
effect). So I looked at some examples in some books I have and they move
the conn.Open() prior to the try block.

Well, that compiles okay. But then what happens if the connection cannot
be opened? That error would not be caught.

Am I missing anything?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com


SqlConnection conn = null;
SqlConnection conn = null;

try

{

conn.Open();

// ...

}

catch(异常错误)

{

// ...

}

最后

{
try
{
conn.Open();
// ...
}
catch (Exception err)
{
// ...
}
finally
{



if(null!= conn)

{

if(null!=conn)
{


conn.Close();
conn.Close();



}

}


}
}



你可以试试,或者使用声明。


" Jonathan Wood" < jw *** @ softcircuits.com写信息

news:u5 ************** @ TK2MSFTNGP02.phx.gbl ...


You can try that, or the "using" statement.


"Jonathan Wood" <jw***@softcircuits.comwrote in message
news:u5**************@TK2MSFTNGP02.phx.gbl...


问候,


我在C#中遇到问题。我正在使用ADO.NET,但这似乎是次要的
Greetings,

I''m having an issue in C#. I''m using ADO.NET but that seems secondary to




the


issue。


以下代码:


SqlConnection conn;

尝试

{

conn.Open();

// ...

}

catch(异常错误)

{

// ...

}

终于

{

conn.Close();

}


不能编译。编译器抱怨conn被使用(在
issue.

The following code:

SqlConnection conn;
try
{
conn.Open();
// ...
}
catch (Exception err)
{
// ...
}
finally
{
conn.Close();
}

Won''t compile. The compiler complains that conn is used (within the



最后

finally


块)未经初始化(或者那种效果)。所以我在一些书中查看了一些例子,他们在try块之前移动了conn.Open()




好​​吧,编译好了。但是如果连接不能
block) without having been initialized (or something to that effect). So I
looked at some examples in some books I have and they move the conn.Open()
prior to the try block.

Well, that compiles okay. But then what happens if the connection cannot




be


打开会发生什么?那个错误不会被抓住。


我错过了什么吗?


谢谢。


-

Jonathan Wood

SoftCircuits编程
http://www.softcircuits.com

opened? That error would not be caught.

Am I missing anything?

Thanks.

--
Jonathan Wood
SoftCircuits Programming
http://www.softcircuits.com



这篇关于尝试抓住问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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