Reputation: 777
I am writing a transaction management system and catching 6 exception I need to rollback transaction for each exception. Now I am doing it in each block, is there a way to centeralized, i know one way is to throw exception from the method.
catch (RollbackException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (HeuristicMixedException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (HeuristicRollbackException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (SystemException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} catch (SystemException e) {
if(log.isErrorEnabled())
{
log.error(e);
}
try {
trans.rollback();
} catch (RollbackException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (HeuristicMixedException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (HeuristicRollbackException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (SystemException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} catch (RollbackException e) {
if(log.isErrorEnabled())
{
log.error(e);
}
try {
trans.rollback();
} catch (RollbackException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (HeuristicMixedException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (HeuristicRollbackException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (SystemException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} catch (HeuristicMixedException e) {
if(log.isErrorEnabled())
{
log.error(e);
}
try {
trans.rollback();
} catch (RollbackException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (HeuristicMixedException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (HeuristicRollbackException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (SystemException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
} catch (HeuristicRollbackException e) {
if(log.isErrorEnabled())
{
log.error(e);
}
try {
trans.rollback();
} catch (RollbackException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (HeuristicMixedException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (HeuristicRollbackException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
} catch (SystemException e1) {
e1.printStackTrace(); //To change body of catch statement use File | Settings | File Templates.
}
}
Upvotes: 0
Views: 472
Reputation: 3346
You can think about whether all of these exceptions are of the same type, and then, if they are, make them subclasses of a bigger superclass.
Edit: I've been informed that you didn't write those exceptions. For a roundabout way, you can use the fact that these are non-RuntimeExceptions (since they're checked exceptions). It's probably not a good habit, and I recommend you rethink your structure instead.
try{
...
catch(Exception e){
if(e instanceof RuntimeException)
throw e; //rethrow unexpected exception
//do the right thing
}
As for restructuring your code, here are a few things I've noticed:
commit()
are rollback exceptions that indicate that the transaction was rolled back.rollback()
method doesn't throw any of those except SystemException
.SystemException
.If you catch an exception, it should be because you'll fix the problem it indicates (more generally, put the program back in a valid state), or you'll do something (like resource cleanup or logging) and rethrow it, or you'll stop the program.
Not knowing the specifics of what you're doing, I'd recommend:
SystemException
at this level. Let it go further.RollbackException
and the HeuristicRollbackException
and log them, but don't roll back.HeuristicMixedException
and roll back.We simplified the code, without even knowing how to catch multiple types of exceptions at the same spot. Using Java 7's two enhancements to exceptions, we can simplify further by doing what you want: catch multiple exceptions at one spot.
Here it is.
try{
try{
trans.commit();
} catch (HeuristicMixedException e) {
if(log.isErrorEnabled()){
log.error(e);
}
trans.rollback(); //catch SystemException at another level
//Note: We are now leaving this catch block as if
// the program state is okay again.
} catch (RollbackException|HeuristicRollbackException e){
if(log.isErrorEnabled()){
log.error(e);
}
//don't need to rollback
//Note: We are now leaving this catch block as if
// the program state is okay again.
}
//let SystemException propagate
}catch(SystemException e){
//!do something
//fix it or rethrow
}
Upvotes: 0
Reputation: 95578
If you are using Java 7, you can reduce all of that to this:
try {
...
} catch(RollbackException|HeuristicMixedException|HeuristicRollbackException e) {
if(log.isErrorEnabled()) {
log.error(e);
}
try {
trans.rollback()
} catch(RollbackException|HeuristicMixedException|HeuristicRollbackException|SystemException e1) {
e1.printStacktrace();
}
}
If you are using Java 6, your options are limited. You can do catch (Exception e)
, but that will catch any kind of exception, which is not what you want. So you will have to do instanceof
checks to make sure that you have the right kind of exception and then throw
the ones that don't match:
try {
...
} catch(Exception e) {
if(e instanceof RollbackException ||
e instanceof HeuristicMixedException ||
e instanceof HeuristicRollbackException) {
if(log.isErrorEnabled()) {
log.error(e);
}
try {
trans.rollback();
} catch(Exception e1) {
if(e1 instanceof RollbackException ||
e1 instanceof HeuristicMixedException ||
e1 instanceof HeuristicRollbackException ||
e1 instanceof SystemException) {
e1.printStacktrace();
} else {
throw e1;
}
}
} else {
throw e;
}
}
Upvotes: 1
Reputation: 6184
If you are using Java 7, you can pipe the exceptions in a single catch:
try {
// code which can throw many kinds of exceptions
}
catch (FooException | BarException | BazException e) {
// do the common thing!
}
Upvotes: 3