try {
fazerAlgumaCoisa();
} catch(Exception e) {
throw new RuntimeException(e);
}
Algumas vezes você realmente quer relançar qualquer exceção verificada como RuntimeException
. O código acima, entretanto, não leva em consideração que uma RuntimeException
estende Exception
. As RuntimeExceptions
não precisavam ser tratadas aqui. A mensagem da exceção também não é propagada apropriadamente. Um código um pouco melhor é tratar RuntimeExceptions
separadamente e não embalá-las. Um código ainda melhor é tratar cada exceção verificada individualmente (mesmo se forem muitas).
try {
fazerAlgumaCoisa();
} catch(RuntimeException e) {
throw e;
} catch(Exception e) {
throw new RuntimeException(
e.getMessage(), e);
}
try {
fazerAlgumaCoisa();
} catch(IOException e) {
throw new RuntimeException(
e.getMessage(), e);
} catch(NamingException e) {
throw new RuntimeException(
e.getMessage(), e);
}