When to use panic

Use panic only for unrecoverable errors where the program cannot safely continue execution.

Use panic only for unrecoverable errors where continuing execution is impossible or unsafe, such as invalid program state or critical initialization failures. For recoverable errors, return an error instead to allow the caller to handle the situation gracefully.

func divide(a, b int) int {
	if b == 0 {
		panic("division by zero is not allowed")
	}
	return a / b
}