Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requests.exceptions.HTTPError vs requests.HTTPError

Is there any reason to prioritize raising/catching requests.exceptions.HTTPError over requests.HTTPError or requests.HTTPError over requests.exceptions.HTTPError?

These two are the same class (As can be seen by reading the source code and by witnessing True is the output of requests.exceptions.HTTPError is requests.HTTPError), yet I ask this question as they both exist.

like image 496
motyzk Avatar asked Feb 22 '26 17:02

motyzk


1 Answers

They both do the same thing. There's no reason to prioritize one over the other.

Try this experiment:

try:
    r = requests.get('http://www.google.com/nothere')
    r.raise_for_status()
except requests.exceptions.HTTPError as e:
    print(e.response.text) 

And then:

try:
    r = requests.get('http://www.google.com/nothere')
    r.raise_for_status()
except requests.HTTPError as e:
    print(e.response.text) 

You'll get the same thing each time:

<!DOCTYPE html>
<html lang=en>
  <meta charset=utf-8>
  <meta name=viewport content="initial-scale=1, minimum-scale=1, width=device-width">
  <title>Error 404 (Not Found)!!1</title>
  <style>
    [***]
  </style>
  <a href=//www.google.com/><span id=logo aria-label=Google></span></a>
  <p><b>404.</b> <ins>That’s an error.</ins>
  <p>The requested URL <code>/nothere</code> was not found on this server.  
  <ins>That’s all we know.</ins>
like image 121
Ollie in PGH Avatar answered Feb 24 '26 06:02

Ollie in PGH



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!