Skip to main content
added 1 character in body
Source Link
Alexander
  • 5.1k
  • 1
  • 24
  • 28

I just finished a discussion with colleagues regarding the use of exceptions in code. Here is the code (pseudocode) that they had written:

resp = fetch(URL)
if resp.status_code != 200:
    return False
resp = fetch(URL)
if resp.status_code != 200:
     return False

I suggested that they use the below (pseudocode):

try:
    resp.fetch(URL)
except HTTPError as e:
    logging.info("HTTP request failed with error: ", e)
    return False
try:
    resp.fetch(URL)
except HTTPError as e:
    logging.info("HTTP request failed with error: ", e)
    return False

To my surprise, I was met with a lot of resistance. Evidently, this team has a policy that they almost never use try/except to manage control flow (I am new to the team). I understand that is an anti-pattern in some cases, but here we are actually handling an exceptional (namely, 4xx or 5xx) response. Also, this is a convention that I have seen used in practically every place I've worked so far.

Does my suggest count as an anti-pattern use of try/except? When is it a good, or bad, idea? I realize that there are a number of posts regarding this topic, and I have read through them; I haven't seen anything specific, though, regarding HTTP request/response handling, which I why I post this question.

I just finished a discussion with colleagues regarding the use of exceptions in code. Here is the code (pseudocode) that they had written:

resp = fetch(URL)
if resp.status_code != 200:
    return False

I suggested that they use the below (pseudocode):

try:
    resp.fetch(URL)
except HTTPError as e:
    logging.info("HTTP request failed with error: ", e)
    return False

To my surprise, I was met with a lot of resistance. Evidently, this team has a policy that they almost never use try/except to manage control flow (I am new to the team). I understand that is an anti-pattern in some cases, but here we are actually handling an exceptional (namely, 4xx or 5xx) response. Also, this is a convention that I have seen used in practically every place I've worked so far.

Does my suggest count as an anti-pattern use of try/except? When is it a good, or bad, idea? I realize that there are a number of posts regarding this topic, and I have read through them; I haven't seen anything specific, though, regarding HTTP request/response handling, which I why I post this question.

I just finished a discussion with colleagues regarding the use of exceptions in code. Here is the code (pseudocode) that they had written:

resp = fetch(URL)
if resp.status_code != 200:
     return False

I suggested that they use the below (pseudocode):

try:
    resp.fetch(URL)
except HTTPError as e:
    logging.info("HTTP request failed with error: ", e)
    return False

To my surprise, I was met with a lot of resistance. Evidently, this team has a policy that they almost never use try/except to manage control flow (I am new to the team). I understand that is an anti-pattern in some cases, but here we are actually handling an exceptional (namely, 4xx or 5xx) response. Also, this is a convention that I have seen used in practically every place I've worked so far.

Does my suggest count as an anti-pattern use of try/except? When is it a good, or bad, idea? I realize that there are a number of posts regarding this topic, and I have read through them; I haven't seen anything specific, though, regarding HTTP request/response handling, which I why I post this question.

Question Protected by gnat
Source Link
Daniel
  • 517
  • 1
  • 4
  • 11

When are try/exceptions not an anti-pattern?

I just finished a discussion with colleagues regarding the use of exceptions in code. Here is the code (pseudocode) that they had written:

resp = fetch(URL)
if resp.status_code != 200:
    return False

I suggested that they use the below (pseudocode):

try:
    resp.fetch(URL)
except HTTPError as e:
    logging.info("HTTP request failed with error: ", e)
    return False

To my surprise, I was met with a lot of resistance. Evidently, this team has a policy that they almost never use try/except to manage control flow (I am new to the team). I understand that is an anti-pattern in some cases, but here we are actually handling an exceptional (namely, 4xx or 5xx) response. Also, this is a convention that I have seen used in practically every place I've worked so far.

Does my suggest count as an anti-pattern use of try/except? When is it a good, or bad, idea? I realize that there are a number of posts regarding this topic, and I have read through them; I haven't seen anything specific, though, regarding HTTP request/response handling, which I why I post this question.