我有一个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
}
}
}
有没有更优雅的方法来做这件事?
Is there a way to ensure that all these methods are called (or attempted) **without nested finally blocks**?
- Sam I am says Reinstate Monica