Mastering exception-handling is of pivotal importance for producing clean and stable Python code. Chances are high you’re already aware of that, as most Python books geared towards newcomers to the language — and often to coding in general — make sure to spend a few paragraphs on the subject.

In essence, **exception-handling means you’re providing an alternative action when a specific piece of code (usually a single line) throws an error for whatever reason. **The attentive coder incorporates exceptions in their script for the same reason they use regex patterns for string matching: your code should harbor as few assumptions as possible. An operation on one type of dataset might not work for a slightly different kind of dataset, and what works today might not work tomorrow. Exception statements are therefore the last safeguard against breaking your code.

The try-except-else-finally clause is a classic and stamped into every aspirant-Python aficionado from day one:

Try:
#Whatever you wish to execute
Except:
#If this throws an error, do the following. Usually the user implements either one of these three steps: 
(a) throw an exception (which breaks the code) or 
(b) perform an alternative action (e.g. record that the try-statement produced an error, but continue with the code anyway)
(c) pass (simply execute the rest of the script)
Else:
#If this doesn't throw an error, do the following
Finally:
#The finally-statement is optional. Whether the try-statement produced an error or not, do the following.

#python #beautifulsoup #web-scraping #digital-methods #scraping

Improve Your Web Scraper With Limited Retry-Loops — Python
4.85 GEEK