开发者

try/catch doesn't work over using statement

开发者 https://www.devze.com 2023-03-06 10:58 出处:网络
try { using (response = (HttpWebResponse)request.GetResponse()) // Exception is not caught by outer try!
try
    {
        using (response = (HttpWebResponse)request.GetResponse())
            // Exception is not caught by outer try!
    }
    catch (Exception ex)
    {
        // Log
    }

EDIT:

// 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);
    }


I could imagine this can happen if you are creating a separate thread within the using block. If an exception is thrown there, be sure to handle it there as well. Otherwise, the outer catch block in this case won't be able to handle it.

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);
        }
    }
}


Do you have more code after the using? The using needs one statement or a block { } after the using statement. In the example below any exception inside the using statement will be caught with the try..catch block.

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


This works fine. You'll see an exception getting printed by the 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()
    {
    }
}

And if you meant that the exception gets thrown inside the using, this works fine to. This will also generate a Console statement:

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()
    {
    }
}


The using keyword is the same as try-catch-finally, http://msdn.microsoft.com/en-us/library/yh598w02.aspx. Basically, you have a try-catch-finally nested inside of a try-catch which is why you're probably so confused.

You could just do this instead...

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();
        }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号