Quindi ho un blocco try/finally. Ho bisogno di eseguire una serie di metodi nel blocco finally. Tuttavia, ognuno di questi metodi può generare un'eccezione. C'è un modo per garantire che tutti questi metodi siano chiamati (o tentati) senza blocchi nidificati finalmente?In java, esiste un modo per garantire che più metodi vengano richiamati in un blocco finale?
Questo è quello che faccio in questo momento, che è abbastanza brutto:
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
}
}
}
C'è un modo più elegante, per fare questo?
È possibile estrarli in un metodo di pulizia. – Reimeus
'C'è un modo per garantire che tutti questi metodi siano chiamati (o tentati) ** senza blocchi finalmente nidificati **?' –