I have a List
of jobs
that takes x number of steps (say 5). Each step must be successful to proceed, if any step fails, then a ticket must be raised and execution of current job
must be forfeited and proceed with next job
.
This is what i currently have (and it works like a charm).
for(Job aJob: jobs){
// Step 1
try{
Obj objRet1 = method1();
// use objRet1;
}
catch(Exception e){
raiseTicket(errMsg1,e);
break;
}
// Step 2
try{
Obj objRet2 = method2();
// use objRet2;
}
catch(Exception e){
raiseTicket(errMsg2,e);
break;
}
// Step 3
try{
Obj objRet3 = method3();
// use objRet3;
}
catch(Exception e){
raiseTicket(errMsg3,e);
break;
}
...
// Step 5
}
This is not very elegant and easily readable, IMO. I would like to condense it to something like below.
for(Job aJob: jobs){
step1();
step2();
..
step5();
}
step1(){
try{
...
}
catch(Exception e){
raiseTicket(errMsg1,e);
break;
}
}
step2(){
}
...
Could someone throw some light on how to improve this program? Please note, returning a value or storing it in method argument also may not work, since, what am trying to achieve here is to avoid the boiler plate code that is required to break
execution and neatly package them in a readable method.