Documentation · Chapter 14

Handle failures and local files deliberately.

Command-line software still receives untrusted input and touches real files. Errors should be understandable and exits should be intentional.

Every lesson starts from the problem, explains the mechanism, then gives a concrete verification.

LESSON OBJECTIVE

What you will be able to do

Handle known failures, paths, writes, and sensitive values without hiding errors or exposing secrets.

Catch only what you can explain

Do not wrap the whole application in a silent catch. Catch a known exception, print a useful message, and preserve a non-zero exit.

phpaml — zsh
$path = __DIR__ . '/../storage/result.txt';
file_put_contents($path, $value, LOCK_EX);

Use paths relative to the project

Use __DIR__ and explicit storage directories. Reject traversal when a user influences a filename, and write atomically when data matters.

Never store plain passwords

password_hash creates a one-way verifier. Encryption is reversible; password storage should not be.

CHECKPOINT

Before you continue

Does every caught exception produce an understandable diagnostic and a non-zero status without revealing sensitive data?