C#的try {}赶上{} [英] C# try {} catch {}

查看:169
本文介绍了C#的try {}赶上{}的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好,感谢您的阅读。即时编程和C#和socket编程的新手。在我的代码我尝试和catch问题,在我的应用程序提供故障tolarence。以下内容:

hi and thanks for reading. im a newbie in programming and C# and sockets programming. in my code i try and catch problems to provide fault tolarence in my app. the following:

        catch (ArgumentNullException e)
        {
            OnNetworkEvents eventArgs = new OnNetworkEvents("Network Unavailable", e.Message);
            OnUpdateNetworkStatusMessage(this, eventArgs);
        }
        catch (EncoderFallbackException e)
        {
            OnNetworkEvents eventArgs = new OnNetworkEvents("Network Unavailable", e.Message);
            OnUpdateNetworkStatusMessage(this, eventArgs);
        }
        catch (SocketException e)
        {
            OnNetworkEvents eventArgs = new OnNetworkEvents("Network Unavailable", e.Message);
            OnUpdateNetworkStatusMessage(this, eventArgs);
        }
        catch (ArgumentOutOfRangeException e)
        {
            OnNetworkEvents eventArgs = new OnNetworkEvents("Network Unavailable", e.Message);
            OnUpdateNetworkStatusMessage(this, eventArgs);
        }
        catch (ObjectDisposedException e)
        {
            OnNetworkEvents eventArgs = new OnNetworkEvents("Network Unavailable", e.Message);
            OnUpdateNetworkStatusMessage(this, eventArgs);
        }



我只是想知道如果我可以用一个单一的替换此重复的代码:

i was just wondering if i can replace this repetitive code with one single:

catch (Exception e) { handle here}

将这项工作?

再次感谢。

推荐答案

没有,永远也赶不上的System.Exception 。我感觉今天像断了纪录,但我真的不能强调这一点。你不能接受 OutOfMemoryException异常 AccessViolationException ,所以不要抓住它!

No, never catch System.Exception. I feel like a broken record today, but I really can't stress this enough. You cannot handle an OutOfMemoryException or AccessViolationException, so don't catch it!

对于示例代码,我有强烈的怀疑,一个 ArgumentNullException ArgumentOutOfRange 例外将对应网络错误。如果这样做,可能意味着真正的组件应该的再追这个异常,是不是。

As for the example code, I have strong doubts that an ArgumentNullException or ArgumentOutOfRange exception would correspond to a network error. If it does, it likely means that the component that really should be catching this exception, isn't.

容错不意思是吃的每一例外,这样应用程序不会崩溃。 - 这意味着实际知道的东西,可以去错了的正确处理的他们

"Fault tolerance" does not mean "eat every exception so the app doesn't crash" - it means actually knowing the things that can go wrong and handling them correctly.

这篇关于C#的try {}赶上{}的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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