Re: Handling multiple exceptions in one exception handler block




Adisht wrote:
Hi all,
I can anyone send me an example of how to catch 2 exceptions but handle
them together ?
Thanks,
Adi

hi,

I had to catch two exceptions, which did not extend the same class, and
log the details of these exceptions. But I did not want to catch all
Exceptions or Throwables that could possibly occur so I did the
following:

1.) Wrapped the code and caught the possible exceptions I want handled

try{
// logic that could throw exception

} catch (Exception1 e1){
handleExceptionWithCommonCode(e1);
} catch (Exception2 e2){
handleExceptionWithCommonCode(e2);
}


2.) Then wrote a private method to handle these exceptions

private void handleExceptionWithCommonCode(Exception e){
// process the exception i.e. log it
}


Am also curious to see if anyone has a neater solution.

Chris

.



Relevant Pages

  • Re: Handling multiple exceptions in one exception handler block
    ... Adisht wrote: ... I can anyone send me an example of how to catch 2 exceptions but handle ... You could say that having a supertype is the way to do this:) This would ... for(Base i: allYourBase) ...
    (comp.lang.java.programmer)
  • Re: Handling multiple exceptions in one exception handler block
    ... Adisht wrote: ... (if the two exceptions do not have a supertype which the other exceptions do not have) this is not possible. ... (Another possibility would be to catch the general class "Exception" and to do an instanceof-check, but this is not a good idea IMHO.) ...
    (comp.lang.java.programmer)