In java, is there a way to make sure that multiple methods are called in the final block?

So I have a try / finally block. I need to execute a series of methods in a finally block. However, each of these methods may raise an exception. Is there a way to ensure that all of these methods are called (or tried) without nested finally blocks?

This is what I'm doing right now, which is pretty ugly:

protected void verifyTable() throws IOException {
    Configuration configuration = HBaseConfiguration.create();
    HTable hTable = null;                                               

    try {
        hTable = new HTable(configuration, segmentMatchTableName);      

        //...
        //various business logic here
        //...

    } finally {                         
        try {
            try {
                if(hTable!=null) {
                    hTable.close(); //This can throw an IOException
                }               
            } finally {
                try {
                    generalTableHelper.deleteTable(configuration, segmentMatchTableName); //This can throw an IOException
                } finally {
                    try {
                        generalTableHelper.deleteTable(configuration, wordMatchTableName); //This can throw an IOException
                    } finally {
                        generalTableHelper.deleteTable(configuration, haplotypeTableName); //This can throw an IOException      
                    }
                }
            }                               
        } finally {
            HConnectionManager.deleteConnection(configuration, true); //This can throw an IOException   
        }
    }               
}

Is there a more elegant way to do this?

+5
source share
8 answers

The standard (working) way to properly manage resources in Java (this principle applies to other languages):

Resource resource = acquire(resource);
try {
    use(resource);
} finally {
    resource.release();
}

Or using a shortcut (with an extra bit of smartness) in the current version of Java SE:

try (Resource resource = acquire(resource)) {
    use(resource);
}

( Joe K, , , Java.)

, :

Resource resource = acquire(resource);
try {
    SubResource sub = resource.acquire();
    try {
        use(sub);
    } finally {
        sub.release();
    }
} finally {
    resource.release();
}

Java SE 7:

try (
    Resource resource = acquire(resource);
    SubResource sub = resource.acquire()
) {
    use(resource, sub);
}

, .

. , , IOException, - , , RuntimeException. Java , Execute Around idiom (. ). Java SE 8 .

with(new ResourceSubAction() { public void use(Resource resource, SubResource sub) {
    ... use resource, sub ...
}});
+2

Java 7, try-with-resources. AutoCloseable.

+2

, . finally.

, , . , , .

0

. . , , , IOException? . , - try-with Java 7. , , . , , .

0

finally, , - , , finally, , try/.

, " " ( , , Jakarta Commons IO). , , , ( deleteTableQuietly()).

JDK-7, try with resource.

0

execute , , , execute. , execute try finally, .

0
deleteTableSilently(table1);    
deleteTableSilently(table2);    
deleteTableSilently(table3);


deleteTableSilently()
    try
        deleteTable()
    catch whatever
        log.error();
0

java.util.concurrent framework - Callable ( ), ExecutorService.invokeAll.

0

All Articles