using () {...}这种结构中,包含异常处理功能吗?using (cls obj=newcls())
{
 .....
}以上这种结构,网友说,相当于下面的代码:try
{}
catch ()
{
}
finally()
{}所以我想问,是不是using 本身就包含的异常处理的功能呀?
也就是说,在using 后面的语句体中,就无须用到 try() catch()语句了,是这样理解吗?

解决方案 »

  1.   

    不包含,using只是在离开using代码块的时候自动调用dispose方法。
      

  2.   

    定义一个范围,将在此范围之外释放一个或多个对象。
    http://msdn.microsoft.com/zh-cn/library/yh598w02(v=VS.80).aspx
      

  3.   

    关键是你要try...catch...干什么?如果你要在catch中给一个变量加1,难道using会未卜先知?using结构自动处理了try...catch....,但是你必须知道是哪个具体的东西。自己google一下,到底是try...catch...中包括具体的什么代码?而不要空洞地说try...catch....。
      

  4.   

    楼上正解,是这样的,using本身并不包含,你自己要具体给出,
    就和你自己说的一样,一般会用
    try{}
    catch(){}的
      

  5.   

    using结构自动处理了try...catch....,但是你必须知道是哪个具体的东西
    --------------------------------
    你说的自动处理了,是什么意思?能举个例子呢?
      

  6.   

    不是try catch,而是相当于try.. finally,释放最终保证是在finally中完成。
      

  7.   

    随便写一个方法        private void test(string connStr)
            {
                using (var conn = new SqlConnection(connStr))
                {
                    int i = 0;
                }        }
    看看编译出什么?private void test(string connStr)
    {
        SqlConnection conn;
        int i;
        bool CS$4$0000;
        conn = new SqlConnection(connStr);
    Label_0008:
        try
        {
            i = 0;
            goto Label_001E;
        }
        finally
        {
        Label_000E:
            if ((conn == null) != null)
            {
                goto Label_001D;
            }
            conn.Dispose();
        Label_001D:;
        }
    Label_001E:
        return;
    }
    Private Sub test(ByVal connStr As String)
        Dim conn As SqlConnection
        Dim i As Integer
        Dim CS$4$0000 As Boolean
        conn = New SqlConnection(connStr)
    Label_0008:
        Try 
            i = 0
            goto Label_001E
        Finally
        Label_000E:
            If (Not (conn Is Nothing) Is Nothing) Then
                goto Label_001D
            End If
            conn.Dispose
        Label_001D:
        End Try
    Label_001E:
        Return
    End Subprivate: void __gc* test(String __gc* connStr)
    {
        SqlConnection __gc* conn;
        Int32 __gc* i;
        Boolean __gc* CS$4$0000;
        conn = __gc new SqlConnection(connStr);
    Label_0008:
        try
        {
            i = 0;
            goto Label_001E;
        }
        finally
        {
        Label_000E:
            if ((conn == 0) != 0)
            {
                goto Label_001D;
            }
            conn->Dispose();
        Label_001D:
        }
    Label_001E:
        return;
    }
    procedure Program.test(connStr: string);
    var
        conn: SqlConnection;
        i: Integer;
        CS$4$0000: boolean;
    begin
        conn := SqlConnection.Create(connStr);Label_0008:
        try
            i := 0;
            goto Label_001E
        finally    Label_000E:
            if ((conn = nil) <> nil) then
                goto Label_001D;
            conn.Dispose
        Label_001D:    end
    Label_001E:
        exit
    end;
    .method private hidebysig instance void test(string connStr) cil managed
    {
        .maxstack 2
        .locals init (
            [0] class [System.Data]System.Data.SqlClient.SqlConnection conn,
            [1] int32 i,
            [2] bool CS$4$0000)
        L_0000: nop 
        L_0001: ldarg.1 
        L_0002: newobj instance void [System.Data]System.Data.SqlClient.SqlConnection::.ctor(string)
        L_0007: stloc.0 
        L_0008: nop 
        L_0009: ldc.i4.0 
        L_000a: stloc.1 
        L_000b: nop 
        L_000c: leave.s L_001e
        L_000e: ldloc.0 
        L_000f: ldnull 
        L_0010: ceq 
        L_0012: stloc.2 
        L_0013: ldloc.2 
        L_0014: brtrue.s L_001d
        L_0016: ldloc.0 
        L_0017: callvirt instance void [mscorlib]System.IDisposable::Dispose()
        L_001c: nop 
        L_001d: endfinally 
        L_001e: nop 
        L_001f: ret 
        .try L_0008 to L_000e finally handler L_000e to L_001e
    }自己看吧,要信自己,不要信csdn。
      

  8.   

    using会处理自身释放时候的异常。
    但是不会代替异常处理。不知道lz问的是什么。