Use this when handling exceptions in scripts.
Exceptions · Error Handling
Exceptions are errors raised by code that can be caught and handled, such as try/catch/finally in PowerShell and Python. Catch specific exceptions and include useful context in the message.
1. Catch specific exceptions.
2. Add context (target, step).
3. Re-raise if not handled.
4. Log tracebacks.
5. Don't hide failures.
exception → handler → log/re-raise
Context & applicabilityCommon mistakes · platform/version boundaries
Operational reference
Fast context before you go deeper.
Verification & Troubleshooting
Start with the observed symptom, prove the nearest boundary, and move outward only when the current state is understood.
Use the operational verification above to establish the expected state.
Capture the concrete output, symptom, packet, log or counter before changing anything.
Step 1 Verification path Verify boundary
The negative fixture should deliberately exercise risks such as ambiguous target identity, stale inputs, partial success, unsafe retry, permission drift and confusing tool completion with target-evidence of the actual state, choosing the cases most relevant to exception taxonomy, propagation, catch scope, cleanup/finally behavior and preserving original cause/traceback rather than swallowing failures.
Step 2 Common mistakes Narrow the fault
- Catching all exceptions and continuing.
Evidence · 1 on this Topic
Expected observations and documentation recipes are reference material. Record actual output, device identity, scope and time before claiming a verified result.
- Catch specific exception types.
- Add context to error messages.
- Re-raise exceptions you cannot handle.
- try/except, try/catch, block/rescue usage — mechanisms
- Specific exception types — precision
- Context in error messages — diagnostics
ReferenceWhy it matters1 section · depth on demand
In depth
An exception is a language's way of signalling that something went wrong, interrupting normal flow until code catches and handles it. Handling exceptions well means catching specific errors where you can do something useful about them, and letting everything else stop the program with a clear message.
Common mistakes and better practice:
| Mistake | Better |
|---|---|
except: / catch {} catching everything | Catch specific exception types |
| Swallowing errors silently | Log with context, then re-raise or handle |
| Losing the original error | Chain it (raise ... from e) |
| Cleanup skipped on error | finally or context managers |
try:
with ConnectHandler(**dev) as conn:
out = conn.send_command("show version")
except NetmikoAuthenticationException:
log.error("auth failed: %s", dev["host"]); raise
except NetmikoTimeoutException:
log.warning("timeout: %s", dev["host"]); failed.append(dev["host"])
ReferenceImportant terms & boundaries1 section · depth on demand
Mechanics
The linked authority for bounded mechanics is Python Documentation — Errors and Exceptions; documentation support is separate from observed lab/production Evidence.
Additional reference context Scope · expected state · examples · history · terminology Open only when you need the supporting context
Verification history & basis
When and against what this Topic was checked. Later text edits do not count as a new check.
Authoritative/upstream documentation reviewed for bounded claims in Error Handling > Exceptions. Documentation verification only; this is not observed lab or production Evidence.
Basis: https://docs.python.org/3/tutorial/errors.html · confidence 94%Deep reference
Healthy vs suspicious evidence
Output interpretation
For exception taxonomy, propagation, catch scope, cleanup/finally behavior and preserving original cause/traceback rather than swallowing failures, prefer typed fields and stable IDs over human presentation text.