是以我有一個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
}
}
}
有更優雅的方式嗎?