# Swallowed errors: `except: pass`, an empty catch, `catch (Exception e) { return null; }`, Go's `if err != nil { return nil }`

*Errors and checks hidden*: one of the ways AI coding agents bloat software, from lessloc's [pattern catalog](https://lessloc.org/patterns/index.md). lessloc catches it as [`swallowed-error`](https://lessloc.org/guide/reference.md#tier-1).

## What makes swallowed errors a bad practice?

The standards that call it a bad practice: [CWE-390](https://cwe.mitre.org/data/definitions/390.html) Detection of Error Condition Without Action; [CWE-1069](https://cwe.mitre.org/data/definitions/1069.html) Empty Exception Block; [CWE-396](https://cwe.mitre.org/data/definitions/396.html).

## What does lessloc do about swallowed errors?

lessloc catches it as [`swallowed-error`](https://lessloc.org/guide/reference.md#tier-1).

- `swallowed-error` (tier 1): An error caught and dropped, such as `except Exception: pass` or an empty catch. The agent hears about it right after the write.

## Examples

Caught as `swallowed-error`:

```js [ac.js]
export function read(f) {
  try {
    return f();
  } catch (e) {
    // ignore
  }
}
```

Written well:

```js [ac.js]
export function read(f) {
  try {
    return f();
  } catch (e) {
    if (e.code !== "ENOENT") throw e;
  }
}
```

Caught as `swallowed-error`:

```go [ad.go]
package ad

func Load(get func() error) error {
	if err := get(); err != nil {
		return nil
	}
	return nil
}

func Save(put func() error) error {
	err := put()
	if err != nil {
		return nil
	}
	return nil
}
```

Written well:

```go [ad.go]
package ad

func Save(put func() error) error {
	err := put()
	if err != nil {
		return err
	}
	return nil
}
```

## Related patterns

The other patterns in [Errors and checks hidden](https://lessloc.org/patterns/index.md#errors-and-checks-hidden):

- [Silenced checks](https://lessloc.org/patterns/silenced-checks.md): lessloc catches it as `suppression`.
- [Checks, fallbacks and catches for cases that cannot happen](https://lessloc.org/patterns/defensive.md): `defensive` catches a null check of a value just created, a type re-checked inside private code and `?.` on a fresh object.
- [Removing validation or error handling to shrink code](https://lessloc.org/patterns/removed-safeguard.md): lessloc catches it as `removed-safeguard`.
