Reputation: 3117
I want to redo the steps if I catch some special exception.
I don't want to copy code again. How can I achieve this?
try{
//step1
//step2
//step3
}catch(specialException1 e){
//redo step1
//redo step2
//redo step3
}
Edited: actually, that exception is thrown from another class due to checking with different condition. And when back to this class, need to handle that and perform that steps again due to requirements. Example like Oauth expired.. so need to refresh the token again.
Upvotes: 0
Views: 44
Reputation: 45070
Firstly, when 1 of those steps threw an Exception
, why do you wanna execute them again?
And if that's a requirement, then you can club those steps and put them in separate method and call that method, in both the try
and catch
blocks.
try{
mySteps();
}catch(SpecialException se){
mySteps();
}
private void mySteps() throws SpecialException{
// Step 1
// Step 2
// Step 3
}
Upvotes: 1
Reputation: 16234
export the steps as a private static method, call them when you want.
If the steps throws checked exceptions, the throws
keyword, should be added to the method signature.
Upvotes: 0