views:

142

answers:

1

suppose am in the middle of updating some information and the network connection goes off.

How should i write my code to handle such situation and resume from where the code was last executing?

One possible approach i could think of is to catch the exeception. and redo the logic again.

While(NetworkProblem){

NetworkProblem = false; try{

//step 1 - Reading info from db //step 2 - storing on class objects //step 3 - updating info //step 4 - connecting to another webserver //step 5 - update info

}catch(NetworkgoneException){ NetworkProblem = true;
// try to connect again - this again i dont know how long will take. }

}

+2  A: 

In principle your approach seems ok. Some points to consider:

  • You don't want to retry indefinitely. Have some maximum retry count, then abort.
  • For bonus points, distinguish errors that are worth retrying from those that are not (e.g. authoritative negative DNS response).
  • Think about what you are trying to accomplish in your communication. What if some data goes through and some does not? Do you need some kind of transaction concept to avoid triggering duplicat actions?
sleske
Avoid busy loop.
iny