Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Print error to screen but continue code execution

I have some code that iterates through a series of URLs. If there is an error in my code because one of the URLs does not contain a valid JSON body, I want the error generated to be printed to screen, but then the code moves onto the next iteration. A simple version of my code is:

for a in myurls:

    try:

        #mycode

    except Exception as exc:

        print traceback.format_exc()
        print exc
        pass

However this prints the error to screen and ends execution of the code. Is there a way I can get the error to continue execution by moving to the next iteration of my 'for' loop?

like image 612
gdogg371 Avatar asked Jun 14 '15 20:06

gdogg371


1 Answers

Just put try-except over the code for which you expect an exception to occur. That code basically lies inside the loop.

for a in myurls:
    try:
        #mycode

    except Exception as exc:

        print traceback.format_exc()
        print exc
like image 122
Animesh Sharma Avatar answered Nov 07 '22 08:11

Animesh Sharma