I am using python for a program where it connects with a database. The python program is fairly simple but it can encounter error if there is issue with database. I want to do something like keep running the code on loop until the database issue is gone. How can I achieve it?
Asked
Active
Viewed 44 times
0
-
use loop, inside that use try and exception – sittsering Sep 09 '21 at 05:40
2 Answers
0
You can try using try and except.
try:
---block of code--
except:
pass
You can use something like this or google for more info on the same.

Vedika Srivastava
- 29
- 1
- 4
-
Hi Vedika, How is pass working over here? Is it re-running the code in try part, if it encounters an error? My exact requirement is I need to re-run the try part. – sanyamjain Sep 09 '21 at 05:52
-
[Why is "except pass" a bad programming practice?](https://stackoverflow.com/questions/21553327/why-is-except-pass-a-bad-programming-practice) – tripleee Sep 09 '21 at 06:09
-
It doesn't re-run the try part for the same sample. pass is similar to continue. If u want to re-run then i think you should put try part as function and call it again in except in case u encounter an error – Vedika Srivastava Sep 09 '21 at 13:11
0
Try this
while True:
try:
#database operation
break
except:
#what to do if an error occurs

KavG
- 169
- 1
- 12