Does it make sense to do “try-finally” without “catch”?

后端 未结 6 1565
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-01-30 03:42

I saw some code like this:

    try
    {
        db.store(mydata);
    }
    finally
    {
        db.cleanup();
    }

I thought try

相关标签:
6条回答
  • 2021-01-30 04:02

    The finally block ensures that even when a RuntimeException is thrown (maybe due to some bug in the called code), the db.cleanup() call will be made.

    This is also often used to prevent too much nesting:

    try
    {
        if (foo) return false;
        //bla ...
        return true;
    }
    finally
    {
        //clean up
    }
    

    Especially when there are many points at which the method returns, this improves readability as anyone can see the clean up code is called in every case.

    0 讨论(0)
  • 2021-01-30 04:07

    Why does this code do it this way?

    Because apparently the code doesn’t know how to handle exceptions at this level. That’s fine – as long as one of the callers does, i.e. as long as the exception gets ultimately handled somewhere.

    Often, low-level code cannot react appropriately to exceptions because the user needs to be notified, or the exception must be logged, or another strategy has to be tried. Low-level code performs one function only and doesn’t know about higher-level decision making.

    But the code still needs to clean up its resources (because if it doesn’t, they would leak), so it does just that in the finally clause, making sure that it always happens, whether an exception was thrown or not.

    0 讨论(0)
  • 2021-01-30 04:17

    The code is doing it to ensure that the database is closed.
    Usually, the way you would do it is to put all your database accessing code in the try block, and then put a call to close the database in the finally block.
    The way try...finally works, means that the code in the try block is run, and the code in the finally block is run when that finishes...no matter what.
    Short of the computer being yanked from the wall, the finally will execute.
    This means that even if an exception is called, and the method takes three years to execute, it will still go in the finally block and the database will be closed.

    0 讨论(0)
  • 2021-01-30 04:20

    If any of the code in the try block can throw a checked exception, it has to appear in the throws clause of the method signature. If an unchecked exception is thrown, it's bubbled out of the method.

    The finally block is always executed, whether an exception is thrown or not.

    0 讨论(0)
  • 2021-01-30 04:27

    This is useful if you want the currently executing method to still throw the exception while allowing resources to be cleaned up appropriately. Below is a concrete example of handling the exception from a calling method.

    public void yourOtherMethod() {
        try {
            yourMethod();
        } catch (YourException ex) {
            // handle exception
        }
    }    
    
    public void yourMethod() throws YourException {
        try {
            db.store(mydata);
        } finally {
            db.cleanup();
        }
    }
    
    0 讨论(0)
  • 2021-01-30 04:28

    It's there because the programmer wanted to make sure that db.cleanup() is called even if the code inside the try block throws an exception. Any exceptions will not be handled by that block, but they'll only be propagated upwards after the finally block is executed. The finally block will also be executed if there was no exception.

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