tinycodinglm / code /bad003.jsonl
winapiadmin's picture
Upload folder using huggingface_hub (part 12)
3c88a0f verified
|
Raw History Blame
2.09 kB
deepseek-harness trace
System You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing c...

You are an expert software engineer and technical assistant. You provide accurate, detailed, and practical answers. When writing code, use best practices, proper error handling, and clear variable names. When analyzing code, identify bugs, security issues, and performance problems. You have deep knowledge of C/C++, Windows API, Python, and many other technologies.

User

What are bad error handling practices?

Assistant

Bad Practices:

  1. Bare Except / Catch All:
python
# Bad
try:
    do_something()
except:
    pass  # silently ignores ALL errors

# Good
try:
    do_something()
except ValueError as e:
    logger.error(f"Invalid value: {e}")
    raise
  1. Swallowing Exceptions:
python
# Bad
try:
    parse_data(raw)
except:
    return None  # caller has no idea what went wrong

# Good
try:
    parse_data(raw)
except ParseError as e:
    raise ValueError(f"Failed to parse: {e}") from e
  1. Generic Error Messages:
python
# Bad
raise Exception("Error occurred")

# Good
raise FileNotFoundError(f"Config not found at {path}")
  1. Returning None Instead of Error:
python
# Bad
def divide(a, b):
    if b == 0:
        return None
    return a / b

# Good
def divide(a, b):
    if b == 0:
        raise ZeroDivisionError("Cannot divide by zero")
    return a / b
  1. Using Exceptions for Flow Control:
python
# Bad
try:
    value = my_dict[key]
except KeyError:
    value = default

# Good
value = my_dict.get(key, default)
  1. Catching Too Broadly:
python
# Bad
try:
    api_call()
except Exception:  # catches KeyboardInterrupt too
    pass

# Good
try:
    api_call()
except (ConnectionError, TimeoutError) as e:
    retry()