Code ReviewIntermediate~15 min

Review: Exception Handling Anti-patterns

pythonexceptionserror-handlinganti-patterns

Code Review Task

Please review the following Python code.

Background

This code provides functionality to fetch and process data from an external API. It needs to handle network errors and data format errors.

Review Criteria

  • Appropriateness of exception handling
  • Use of exceptions
  • Quality of error messages
  • Preservation of exception chain

Your Answer

api_client.pypython
import requests
import json
def fetch_api_data(url):
"""Fetch data from API"""
try:
response = requests.get(url)
return response.json()
except Exception as e:
raise ValueError("Failed to fetch data")
def process_data(data):
"""Process API data"""
try:
result = {
"id": data["id"],
"value": int(data["value"]) * 2
}
return result
except:
print("Error processing data")
return {}
行番号をクリックしてコメントを追加(Shift+クリックで範囲選択)

Minimum 100 characters required

0

Add comments and enter at least 100 characters in the summary

Model Answer

Submit your answer or click "Show" to view the model answer.