When using return, break or continue in a finally block, it will stop the spread of any exceptions that were thrown in the try, else, or except blocks and will disregard any return statements.
try:client_obj.get_url(url)except(URLError,ValueError):client_obj.remove_url(url)exceptSocketTimeout:client_obj.handle_url_timeout(url)finally:break# avoid break in the finally block
try:client_obj.get_url(url)except(URLError,ValueError):client_obj.remove_url(url)exceptSocketTimeout:client_obj.handle_url_timeout(url)finally:return0# avoid return in the finally block
try:client_obj.get_url(url)except(URLError,ValueError):client_obj.remove_url(url)exceptSocketTimeout:client_obj.handle_url_timeout(url)finally:continue# avoid continue in the finally block
Compliant Code Examples
try:client_obj.get_url(url)except(URLError,ValueError):client_obj.remove_url(url)exceptSocketTimeout:client_obj.handle_url_timeout(url)finally:print("cleanup the mess")
Seamless integrations. Try Datadog Code Analysis
Datadog Code Analysis
Try this rule and analyze your code with Datadog Code Analysis
How to use this rule
1
2
rulesets:- python-best-practices # Rules to enforce Python best practices.
Create a static-analysis.datadog.yml with the content above at the root of your repository
Use our free IDE Plugins or add Code Analysis scans to your CI pipelines