所以我有一個嘗試/ finally塊。我需要在finally塊中執行一些方法。但是,這些方法中的每一個都會引發異常。有沒有辦法確保所有這些方法都被調用(或試圖)沒有嵌套finally塊?在java中,有沒有辦法確保在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
}
}
}
是否有一個更優雅的方式來做到這一點?
你可以將它們解壓縮到清理方法中。 – Reimeus
'有沒有辦法確保所有這些方法被調用(或企圖)**沒有嵌套finally塊**?' –