try / catch不能使用语句

时间:2011-05-18 02:32:57

标签: c# try-catch using-statement

try
    {
        using (response = (HttpWebResponse)request.GetResponse())
            // Exception is not caught by outer try!
    }
    catch (Exception ex)
    {
        // Log
    }

编辑:

// Code for binding IP address:
ServicePoint servicePoint = ServicePointManager.FindServicePoint(uri);
servicePoint.BindIPEndPointDelegate = new BindIPEndPoint(Bind);
//
private IPEndPoint Bind(ServicePoint servicePoint, IPEndPoint remoteEndPoint, int retryCount)
    {
        IPAddress address;

        if (retryCount < 3)
            address = IPAddress.Parse("IPAddressHere");
        else
            {
                address = IPAddress.Any;
                throw new Exception("IP is not available,"); // This exception is not caught
            }

        return new IPEndPoint(address, 0);
    }

4 个答案:

答案 0 :(得分:2)

我可以想象如果你在using块中创建一个单独的线程,就会发生这种情况。如果抛出异常,请务必在那里处理它。否则,在这种情况下,外部捕获块将无法处理它。

class TestClass : IDisposable
{
    public void GetTest()
    {
        throw new Exception("Something bad happened"); // handle this
    }

    public void Dispose()
    {
    }
}

class Program
{
    static void Main(string[] args)
    {
        try
        {
            using (TestClass t = new TestClass())
            {
                Thread ts = new Thread(new ThreadStart(t.GetTest));
                ts.Start();
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine("Error: " + ex.Message);
        }
    }
}

答案 1 :(得分:1)

使用后你有更多的代码吗? using语句之后需要一个语句或一个块{}。在下面的示例中,using语句中的任何异常都将被try..catch块捕获。

try
{
    using (response = (HttpWebResponse)request.GetResponse())
    {
        ....
    }
}
catch (Exception ex)
{
}

答案 2 :(得分:1)

这很好用。您将看到Console.WriteLine()

打印的异常
class Program
{
    static void Main(string[] args)
    {
        Foo foo = new Foo();

        try
        {
            using (Bar bar = foo.CreateBar())
            {

            }
        }
        catch(Exception exception)
        {
            Console.WriteLine(exception.Message);
        }
    }
}

public class Foo
{
    public Bar CreateBar()
    {
        throw new ApplicationException("Something went wrong.");
    }
}

public class Bar : IDisposable
{
    public void Dispose()
    {
    }
}

如果你的意思是在使用中抛出异常,这可以正常工作。这也将生成一个Console语句:

class Program
{
    static void Main(string[] args)
    {
        Foo foo = new Foo();

        try
        {
            using (Bar bar = foo.CreateBar())
            {
                throw new ApplicationException("Something wrong inside the using.");
            }
        }
        catch(Exception exception)
        {
            Console.WriteLine(exception.Message);
        }
    }
}

public class Foo
{
    public Bar CreateBar()
    {
        return new Bar();
        // throw new ApplicationException("Something went wrong.");
    }
}

public class Bar : IDisposable
{
    public void Dispose()
    {
    }
}

答案 3 :(得分:1)

using关键字与try-catch-finally,http://msdn.microsoft.com/en-us/library/yh598w02.aspx相同。基本上,你有一个try-catch-finally嵌套在try-catch中,这就是为什么你可能会这么困惑。

你可以这样做......

class Program
{
    static void Main(string[] args)
    {
        HttpWebResponse response = new HttpWebResponse();
        try
        {
            response.GetResponse();
        }
        catch (Exception ex)
        {
            //do something with the exception
        }
        finally
        {
            response.Dispose();
        }
    }
}