所以我有一个尝试/ 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块**?' –