How to Properly Catch and Raise Exceptions

🐍 How to Properly Catch and Raise Exceptions – A Guide for Junior Developers

(This article was created by Qwen AI)

Handling exceptions correctly isn’t just about preventing crashes β€” it makes your code more stable, easier to debug, and simpler to maintain. Here are simple, practical rules every junior developer should follow.


❌ Don’t catch an exception just “because you can”.

1
2
3
4
5
def c():
    try:
        save_data()
    except OSError:
        pass  # ❌ Wrong! This "swallows" the error β€” no one will know something went wrong

βœ… Only catch it if you can handle it meaningfully:

1
2
3
4
try:
    load_config()
except FileNotFoundError:
    return DEFAULT_CONFIG  # βœ… Good: use default if file is missing

When you’re in a low-level function (e.g., writing to a file, calling an API), you might want to log the error for debugging, but if you can’t fully handle it, you must re-raise.

1
2
3
4
5
6
def c():
    try:
        write_to_file(data)
    except OSError as e:
        logging.error(f"[c()] Failed to write file: {e}")
        raise  # βœ… Important: propagate the error up for higher-level handling

πŸ”Ή Using raise (without arguments) preserves the original traceback β€” very helpful for debugging later.


βœ… Higher-level functions (like a()) are usually closest to the user, so they’re the best place to:

  • Show user-friendly error messages.
  • Suggest actions (retry, check permissions, etc.).
  • Log high-level system events.
1
2
3
4
5
def a():
    try:
        b()
    except OSError:
        print("Failed to save data. Please check your disk space.")

Only when you’ve fully resolved the issue and the program can safely continue:

1
2
3
4
5
def get_user_setting():
    try:
        return read_from_file("setting.txt")
    except FileNotFoundError:
        return "default"  # βœ… Valid: no file β†’ use default value

β†’ In this case, the error is not a failure β€” it’s part of a valid execution path.


  • ❌ Catch an exception and just pass or use raise Exception("...") (this loses the traceback).
  • ❌ Catch the same error at multiple levels without adding value.
  • ❌ Log and then stay silent β€” if you can’t handle it, don’t hide it.

  1. Don’t catch if you don’t know what to do.
  2. Logged the error? Still raise if it’s not fully handled.
  3. Handle the error where you have user or system context.

πŸ“Œ Remember: Exceptions are not bad β€” what matters is that you handle them responsibly.


β€” Written by a dev who once swallowed an exception and spent 3 hours debugging πŸ˜…

Related Content