I have a task where I need to handle "Unhandled Exception" without using "throws XXXException". Here is the situation:
I need to retry a method if it throws JMSException.
public void retry(int retryCount) throws MessageGatewayException, JMSException {
restartConnection(); //
}
public void restartConnection() throws JMSException {
init(); //this is where JMSException is thrown
}
and on my send method, I call retry().
public void send(int retryCount) throws MessageGatewayException {
//exit method if retried enough times.
if(retryCount > someNumber)
throw new IllegalArgumentException();
try {
producer.send();
}
catch (JMSException e) {
retry(retryCount); // this is where Unhandled Exception is.
}
}
What I want to do is when retry() is called from send(), and if restartConnection() is failed and threw JMSException, I want retry() to call send(retryCount++). I don't want to add JMSException on send() method, but it says I need to implement it. Is there way to handle the exception so I don't have JMSException on send()?
You could call the method with retryCount+1
but when you reach, for example 10, raise the exception to avoid infinite error
Handle it in send
public void send(int retryCount) throws MessageGatewayException {
//exit method if retried enough times.
if(retryCount > someNumber)
throw new IllegalArgumentException();
try {
producer.send();
}
catch (JMSException e) {
try {
retry(retryCount); // this is where Unhandled Exception is
}catch (JMSException ex) {
if(retryCount < 10)
send(retryCount+1);
else
logger.error(e); // or print or whatever
}
}
}
Handle it in retry
:
public void retry(int retryCount) throws MessageGatewayException{
try{
restartConnection(); //
}catch (JMSException e) {
if(retryCount < 10)
retry(retryCount+1);
else
logger.error(e); // or print or whatever
}
}
So I found a way to handle this, but I am not sure this is normal thing to do. Here is what I did:
public void send(int retryCount) throws MessageGatewayException {
//exit method if retried enough times.
if(retryCount > someNumber)
throw new IllegalArgumentException();
try {
producer.send();
}
catch (JMSException e) {
try {
retry(retryCount); // this is where Unhandled Exception is
}
catch (JMSException ex) {
send(++retryCount);
}
}
}
So I am basically using another try/catch block inside of my catch block. This does remove the error, and it works fine, but I am not sure this is the way to go.
Seems like keeping the retry logic contained in the retry
method is desirable. This assumes the initial retryCount
parameter to send
is a "maximum number of retries to attempt".
public void retry(int retryCount) throws MessageGatewayException {
while (retryCount-- > 0 && !restartConnection());
}
And add Boolean result to restartConnection
:
public boolean restartConnection() {
try {
init();
return true;
} catch (JMSException e) {
return false;
}
}
Your send
method is unchanged in this example.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.