Bird
0
0

Why is it important to use HTTPException instead of returning a custom error dictionary with status code 400 in FastAPI?

hard🧠 Conceptual Q10 of 15
FastAPI - Error Handling
Why is it important to use HTTPException instead of returning a custom error dictionary with status code 400 in FastAPI?
AHTTPException properly stops execution and sets HTTP status code in response
BReturning a dictionary automatically sets the status code
CHTTPException logs errors to the console automatically
DThere is no difference; both behave the same
Step-by-Step Solution
Solution:
  1. Step 1: Understand HTTPException effect on request flow

    Raising HTTPException stops endpoint execution and sets the HTTP status code in the response.
  2. Step 2: Compare with returning custom dict

    Returning a dict does not change HTTP status code unless explicitly set, and does not stop execution.
  3. Final Answer:

    HTTPException properly stops execution and sets HTTP status code in response -> Option A
  4. Quick Check:

    Use HTTPException to stop and set HTTP status code correctly [OK]
Quick Trick: HTTPException stops execution and sets HTTP status code [OK]
Common Mistakes:
MISTAKES
  • Thinking returning dict sets status code automatically
  • Assuming HTTPException only logs errors
  • Believing both methods behave identically

Want More Practice?

15+ quiz questions · All difficulty levels · Free

Free Signup - Practice All Questions
More FastAPI Quizzes