How to handle the code for the network failure and get back to work?
suppose am is in the middle of updating some information and the network connection goes down.
How do I write my code to handle such a situation and resume from where the last code ran?
One possible approach I could think of is a gimmick. and repeat the logic again.
While (NetworkProblem) {
NetworkProblem = false; try {
// Step 1 - Read information from db // step 2 - save class objects // step 3 - update information // step 4 - connect to another web server // step 5 - update information
} catch (NetworkgoneException) {NetworkProblem = true,
// try reconnecting - again I don't know how long it will take. }
}
0
a source to share
1 answer
Basically your approach looks ok. Some points to consider:
- You don't want to try again indefinitely. Have the maximum number of retry attempts and then abort.
- For bonus points, distinguish between mistakes that are worth repeating from those that are not (for example, an authoritative negative DNS response).
- Think about what you are trying to achieve in your communication. What if some data goes through and some does not? Do you need some kind of transaction concept to avoid triggering duplicates?
+2
a source to share