Why is try {…} finally {…} good; try {…} catch{} bad?

后端 未结 20 2200
执笔经年
执笔经年 2020-11-28 01:30

I have seen people say that it is bad form to use catch with no arguments, especially if that catch doesn\'t do anything:

StreamReader reader=new  StreamRead         


        
相关标签:
20条回答
  • 2020-11-28 01:48

    Amongst probably many reasons, exceptions are very slow to execute. You can easily cripple your execution times if this happens a lot.

    0 讨论(0)
  • 2020-11-28 01:49

    Taken from: here

    Raising and catching exceptions should not routinely occur as part of the successful execution of a method. When developing class libraries, client code must be given the opportunity to test for an error condition before undertaking an operation that can result in an exception being raised. For example, System.IO.FileStream provides a CanRead property that can be checked prior to calling the Read method, preventing a potential exception being raised, as illustrated in the following code snippet:

    Dim str As Stream = GetStream() If (str.CanRead) Then 'code to read stream End If

    The decision of whether to check the state of an object prior to invoking a particular method that may raise an exception depends on the expected state of the object. If a FileStream object is created using a file path that should exist and a constructor that should return a file in read mode, checking the CanRead property is not necessary; the inability to read the FileStream would be a violation of the expected behavior of the method calls made, and an exception should be raised. In contrast, if a method is documented as returning a FileStream reference that may or may not be readable, checking the CanRead property before attempting to read data is advisable.

    To illustrate the performance impact that using a "run until exception" coding technique can cause, the performance of a cast, which throws an InvalidCastException if the cast fails, is compared to the C# as operator, which returns nulls if a cast fails. The performance of the two techniques is identical for the case where the cast is valid (see Test 8.05), but for the case where the cast is invalid, and using a cast causes an exception, using a cast is 600 times slower than using the as operator (see Test 8.06). The high-performance impact of the exception-throwing technique includes the cost of allocating, throwing, and catching the exception and the cost of subsequent garbage collection of the exception object, which means the instantaneous impact of throwing an exception is not this high. As more exceptions are thrown, frequent garbage collection becomes an issue, so the overall impact of the frequent use of an exception- throwing coding technique will be similar to Test 8.05.

    0 讨论(0)
  • 2020-11-28 01:52

    It's bad practice to add a catch clause just to rethrow the exception.

    0 讨论(0)
  • 2020-11-28 01:53

    Finally is optional -- there's no reason to have a "Finally" block if there are no resources to clean up.

    0 讨论(0)
  • 2020-11-28 01:56

    Use Try..Catch..Finally, if your method knows how to handle the exception locally. The Exception occurs in Try, Handled in Catch and after that clean up is done in Finally.

    In case if your method doesn't know how to handle the exception but needs a cleanup once it has occurred use Try..Finally

    By this the exception is propagated to the calling methods and handled if there are any suitable Catch statements in the calling methods.If there are no exception handlers in the current method or any of the calling methods then the application crashes.

    By Try..Finally it is ensured that the local clean up is done before propagating the exception to the calling methods.

    0 讨论(0)
  • 2020-11-28 01:57

    If you don't know what exception type to catch or what to do with it, there's no point in having a catch statement. You should just leave it for a higher-up caller that may have more information about the situation to know what to do.

    You should still have a finally statement in there in case there is an exception, so that you can clean up resources before that exception is thrown to the caller.

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