Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/337.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# try/catch不';不要反复使用语句_C#_Try Catch_Using Statement - Fatal编程技术网

C# try/catch不';不要反复使用语句

C# try/catch不';不要反复使用语句,c#,try-catch,using-statement,C#,Try Catch,Using Statement,编辑: //绑定IP地址的代码: ServicePoint ServicePoint=ServicePointManager.FindServicePoint(uri); servicePoint.BindIPEndPointDelegate=新的BindIPEndPoint(绑定); // 专用IPEndPoint绑定(ServicePoint ServicePoint、IPEndPoint remoteEndPoint、int retryCount) { IP地址; 如果(retryCoun

编辑:

//绑定IP地址的代码:
ServicePoint ServicePoint=ServicePointManager.FindServicePoint(uri);
servicePoint.BindIPEndPointDelegate=新的BindIPEndPoint(绑定);
//
专用IPEndPoint绑定(ServicePoint ServicePoint、IPEndPoint remoteEndPoint、int retryCount)
{
IP地址;
如果(retryCount<3)
address=IPAddress.Parse(“IPAddressHere”);
其他的
{
地址=IPAddress.Any;
抛出新异常(“IP不可用”);//未捕获此异常
}
返回新的IPEndPoint(地址,0);
}

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

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

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

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

这个很好用。您将看到控制台打印了一个异常

如果您的意思是在using中抛出异常,那么这对您来说很好。这还将生成一个控制台语句:

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

这个很好用。您将看到控制台打印了一个异常

如果您的意思是在using中抛出异常,那么这对您来说很好。这还将生成一个控制台语句:

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

我可以想象,如果使用块在
中创建一个单独的线程,就会发生这种情况。如果在那里抛出异常,请确保也在那里处理它。否则,在这种情况下,外部挡块将无法处理它

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

我可以想象,如果使用
块在
中创建一个单独的线程,就会发生这种情况。如果在那里抛出异常,请确保也在那里处理它。否则,在这种情况下,外部挡块将无法处理它

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

using关键字与try catch finally相同。基本上,您有一个try-catch,它最终嵌套在try-catch中,这就是为什么您可能如此困惑的原因

你可以这样做

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

using关键字与try catch finally相同。基本上,您有一个try-catch,它最终嵌套在try-catch中,这就是为什么您可能如此困惑的原因

你可以这样做

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

那不是真的。那么你是说异常被“吞没”,还是说异常从catch子句中冒泡出来?在VisualStudio中它不起作用。如果发生异常,则表示发生了未处理的异常。但是在VisualStudio之外运行时,一切都正常。注意:委托调用是在内部使用触发的,这将导致异常。请发布一段代码片段来演示该问题。在下面的回答中,我列举了一个可能发生未处理异常的场景。如果你是这样的话,请告诉我。通常,您应该在using块中处理异步方法。否则,外部挡块将无法为您处理它。是的,VS会让您知道任何未处理的异常。这不是真的。那么您是说异常被“吞没”,还是异常从catch子句中冒泡出来?在visual studio中,它不起作用。如果发生异常,则表示发生了未处理的异常。但是在VisualStudio之外运行时,一切都正常。注意:委托调用是在内部使用触发的,这将导致异常。请发布一段代码片段来演示该问题。在下面的回答中,我列举了一个可能发生未处理异常的场景。如果你是这样的话,请告诉我。通常,您应该在using块中处理异步方法。否则,外部挡块将无法为您处理它。是的,VS会让您知道任何未处理的异常。使用
IPEndPoint
绑定IP地址似乎会导致
回调
在我无法控制的另一个线程上运行。如果IP不可用且出现异常,尽管调用方在try/catch中,但异常会被视为
未处理
。是的,
回调
可能需要正确处理。使用
IPEndPoint
绑定IP地址似乎会导致
回调
在我无法控制的另一个线程上运行。如果IP不可用且发生异常,尽管调用方在try/catch中,但异常将被视为
未处理
。是的,
回调
需要正确处理。