C# try {} catch {}

后端 未结 11 2419
既然无缘
既然无缘 2021-02-08 10:51

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:

相关标签:
11条回答
  • 2021-02-08 11:04

    While you can do that , I'd consider it poor practice and not how you'd want to be structuring your code.

    Depending on the exception you may want to do something different. An invalid IP is a different issue than a hardware error and so on. Additionally some errors you might want to notify back to the UI via a delegate or log somewhere using log4net.

    But that's just me and I'm far from an expert - so take it for what it's worth

    0 讨论(0)
  • 2021-02-08 11:09

    IMO:

    Catching Exception would be pretty similar to having methods with parameters that are never more specific of a class than Object; Sure you can do it but is it really the best decision 9 out of 10 times?

    What you should consider doing to simplify your code some is Catch exceptions at the highest definition that is relevant.

    For instance, in java I would

    try{...}
    catch( IOException e ) {...}
    

    to catch all IO related exceptions.

    There are too many things that can throw too many different types of exceptions, you typically should avoid using the catch-all that is Exception.

    0 讨论(0)
  • 2021-02-08 11:14

    That will certainly catch all exceptions, but that can be poor practice. There are some exceptions that will be hidden or misrepresented that will make testing and debugging your application much more difficult.

    0 讨论(0)
  • 2021-02-08 11:17

    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!

    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.

    0 讨论(0)
  • 2021-02-08 11:19

    You could but you would lose granularity and the ability to handle each issue individually, which isn't the best practice. Usually you would handle different types of exceptions first, just as you have, then if you need to do something with any unhandled exceptions you could add the Exception at the end. Re-throw it once you're done to preserve the stack-trace and let it bubble up.

    Keep what you have an add a base case at the end:

    catch (ArgumentNullException e) { ... }
    catch (EncoderFallbackException e)  { ... }
    catch (SocketException e) { ... }
    catch (ArgumentOutOfRangeException e)  { ... }
    catch (ObjectDisposedException e) { ... }
    catch (Exception e)
    {
        // not handled by above exceptions, do something if needed
        throw; // after doing what you need, re-throw it
    }
    
    0 讨论(0)
  • 2021-02-08 11:19

    Yes it would work, but it would not do the same, since all exceptions and not only the specific ones would be catched.

    This is usually not what you want, as the general rule is to catch only exceptions which you know how to handle. There are some situations where you need such a catch-all to redirect the exception (like accross thread boundaries or in async calls) or do some cleanup work if something - anything - goes wrong (but then you usually re-throw the exception after doing your work).

    0 讨论(0)
提交回复
热议问题