How to use try Exception block to save my code from abnormal termination?
-
06-03-2021 - |
Question
I'm writing a program that connects to my website via ftp. If the login authentication fails, I want it to continue on to do something else. But an authentication failure error automatically exits the program. Is there a way to ignore this and continue on?
Solution
You can use Python's Exception Handling:
try:
do_something()
except Exception, ex:
print "The process failed because", ex
do_something_else()
replace do_something() with code you first want to try.. Replace do_somthing_else() code with what you wish to try when do_something() part fails
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow