Handle Exceptions to prevent crashes in Python

InstructorWill Button

Share this video with your friends

Send Tweet

Exceptions cause your application to crash. Handling them allows you to recover gracefully and keep your application running. Learn how to handle exceptions, find what exceptions you should be handling, and exit gracefully in this lesson. You will also learn how you should not handle exceptions and when it is better to let your application crash rather than swallowing an exception.

Victor Hazbun
~ 6 years ago

How do you catch an exception from the requests.exceptions "ReadTimeout" ? Can you please show me a quick example?

Victor Hazbun
~ 6 years ago

How do you catch an exception from the requests.exceptions "ReadTimeout" ? Can you please show me a quick example?

try:
    r = requests.get(url, params={'s': thing})
except requests.exceptions.RequestException as e:  # This is the correct syntax
    print e
    sys.exit(1)