I had to start coding on python without any education in coding. So my skill is shame. 
I have a telegram bot. Sometimes it crashes, I don't know why and actually it doesn't matter, but I need to make bot restart itself when it crashes because of some weird loss of data. So please explain me how to add while True or try-except. Please, teach me where should I put these constructions in my code. All the code has to be inside try statement, am I right? I am really sorry for dumb questions, but I need your help. Thank you!
            Asked
            
        
        
            Active
            
        
            Viewed 335 times
        
    -3
            
            
         
    
    
        Barbaros Özhan
        
- 59,113
- 10
- 31
- 55
 
    
    
        Boris Diaz
        
- 1
- 1
- 
                    1You'd be best off reading a tutorial or two. And you should fix the underlying problem rather than restarting on failure. – mhawke Oct 09 '17 at 05:34
- 
                    1This is not a personalized on demand tutorial site. Just go read existing tutorials first and come back if you have a specific question that is not already addressed elsewhere. – Julien Oct 09 '17 at 05:36
2 Answers
2
            
            
        If you're using linux, then you could write an independent bash script that would restart your python script in case of crashes and keep it running:
until foo.py; do
    echo "'foo.py' crashed with exit code $?.  Respawning.." >&2
    sleep 1
done
You will want to run this bash script in the background:
nohup ./bar.sh &
(Source)
 
    
    
        udhavsethi
        
- 63
- 7
-1
            
            
        Going off Python: about catching ANY exception
You could do something like the following:
while (true):
    try:
        #your code here
    except:
       print "Caught an exception, discarding it."
Note that this catches ANY exception, which isn't a good idea.
 
    
    
        pellucidcoder
        
- 127
- 3
- 9