天天看点

java finally块_在java中,有没有办法确保在finally块中调用多个方法?

所以我有一个try / finally块.我需要在finally块中执行许多方法.但是,这些方法中的每一个都可以抛出异常.有没有办法确保在没有嵌套的finally块的情况下调用(或尝试)所有这些方法?

这就是我现在所做的,这非常难看:

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

}

}

}

有更优雅的方式吗?