Thursday, 5 September 2013

Handling recoverable and unrecoverable exceptions

Handling recoverable and unrecoverable exceptions

Hopefully I can explain this clearly. If I have a main method with lots of
steps that can generate different exceptions, some fatal, some not, do I
have to catch the "recoverable" ones separately? It seems like that would
result in potentially a lot of try/catch blocks, like so:
public static void main (String[] args) {
try {
//...
for (int i=0;someArray.length;i++) {
try{
System.out.println("I = " + i);
doSometing(i);
} catch (RecoverableException e) {
//recover, continue, whatever
//log warning
//keep
}
}//end for loop
try {
doSomethingElse();
} catch (AnotherRecoverableException e) {
//not fatal, keep on chugging
}
//...
//do more stuff that can throw unrecoverable exceptions
} catch (UnrecoverableException e) {
System.out.println("I can't handle this, it's too much!");
e.printStackTrace();
}
}
Is there a better way to do this?

No comments:

Post a Comment