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

后端 未结 20 2211
执笔经年
执笔经年 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:57

    From a readability perspective, it's more explicitly telling future code-readers "this stuff in here is important, it needs to be done no matter what happens." This is good.

    Also, empty catch statements tend to have a certain "smell" to them. They might be a sign that developers aren't thinking through the various exceptions that can occur and how to handle them.

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

    If you'll read C# for programmers you will understand, that the finally block was design to optimize an application and prevent memory leak.

    The CLR does not completely eliminate leaks... memory leaks can occur if program inadvertently keep references to unwanted objects

    For example when you open a file or database connection, your machine will allocate memory to cater that transaction, and that memory will be kept not unless the disposed or close command was executed. but if during transaction, an error was occurred, the proceeding command will be terminated not unless it was inside the try.. finally.. block.

    catch was different from finally in the sense that, catch was design to give you way to handle/manage or interpret the error it self. Think of it as person who tells you "hey i caught some bad guys, what do you want me to do to them?" while finally was designed to make sure that your resources was properly placed. Think of it of someone that whether or not there is some bad guys he will make sure that your property was still safe.

    And you should allow those two to work together for good.

    for example:

    try
    {
      StreamReader reader=new  StreamReader("myfile.txt");
      //do other stuff
    }
    catch(Exception ex){
     // Create log, or show notification
     generic.Createlog("Error", ex.message);
    }
    finally   // Will execute despite any exception
    {
      reader.Close();
    }
    
    0 讨论(0)
  • 2020-11-28 01:57

    The problem with try/catch blocks that catch all exceptions is that your program is now in an indeterminate state if an unknown exception occurs. This goes completely against the fail fast rule - you don't want your program to continue if an exception occurs. The above try/catch would even catch OutOfMemoryExceptions, but that is definitely a state that your program will not run in.

    Try/finally blocks allow you to execute clean up code while still failing fast. For most circumstances, you only want to catch all exceptions at the global level, so that you can log them, and then exit out.

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

    The big difference is that try...catch will swallow the exception, hiding the fact that an error occurred. try..finally will run your cleanup code and then the exception will keep going, to be handled by something that knows what to do with it.

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

    Well for one, it's bad practice to catch exceptions you don't bother to handle. Check out Chapter 5 about .Net Performance from Improving .NET Application Performance and Scalability. Side note, you should probably be loading the stream inside the try block, that way, you can catch the pertinent exception if it fails. Creating the stream outside the try block defeats its purpose.

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

    "Finally" is a statement of "Something you must always do to make sure program state is sane". As such, it's always good form to have one, if there's any possibility that exceptions may throw off the program state. The compiler also goes to great lengths to ensure that your Finally code is run.

    "Catch" is a statement of "I can recover from this exception". You should only recover from exceptions you really can correct - catch without arguments says "Hey, I can recover from anything!", which is nearly always untrue.

    If it were possible to recover from every exception, then it would really be a semantic quibble, about what you're declaring your intent to be. However, it's not, and almost certainly frames above yours will be better equipped to handle certain exceptions. As such, use finally, get your cleanup code run for free, but still let more knowledgeable handlers deal with the issue.

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