{"record":{"id":"ff3f50ed257edf46","repo":"unknwon/the-way-to-go_ZH_CN","slug":"error-occurred-err-error-ff3f50","errorCode":null,"errorMessage":"\"ERROR occurred: \" + err.Error()","messagePattern":"\"ERROR occurred: \" \\+ err\\.Error\\(\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"eBook/18.10.md","lineNumber":16,"sourceCode":"# 18.10 其他\r\n\r\n如何在程序出错时终止程序：\r\n\r\n```go\t\r\nif err != nil {\r\n   fmt.Printf(\"Program stopping with error %v\", err)\r\n   os.Exit(1)\r\n}\r\n```\r\n\r\n或者：\r\n\r\n```go\r\nif err != nil { \r\n\tpanic(\"ERROR occurred: \" + err.Error())\r\n}\r\n```\r\n\r\n## 链接\r\n\r\n- [目录](directory.md)\r\n- 上一节：[网络和网页应用](18.9.md)\r\n- 下一节：[出于性能考虑的最佳实践和建议](18.11.md)\n","sourceCodeStart":1,"sourceCodeEnd":25,"githubUrl":"https://github.com/unknwon/the-way-to-go_ZH_CN/blob/7a54d34d3657084b6a59e5618bd069b912d571aa/eBook/18.10.md#L1-L25","documentation":"Section 18.10 presents two termination styles for unrecoverable errors: print-and-os.Exit(1), or panic(\"ERROR occurred: \" + err.Error()). The panic variant crashes with a full stack trace — that is the point: maximum debugging information at the cost of a hard, unclean abort.","triggerScenarios":"Any err != nil reaching this branch at a point where the program decides continuation is impossible; the section frames it as the alternative to os.Exit for must-stop situations.","commonSituations":"Final-mile error handling in main after retries are exhausted; porting shell scripts whose set -e / exit-on-error semantics are being mimicked; cleanup code accidentally skipped because panic bypasses it.","solutions":["Prefer returning/propagating the error and deciding at top level how to exit","Use log.Fatalf(\"ERROR occurred: %v\", err) when a clean exit with a log line is acceptable","Keep the panic only when you specifically want the goroutine dump for diagnosis","Wrap third-party call sites in deferred recover when interop code panics this way"],"exampleFix":"// before\nif err != nil {\n    panic(\"ERROR occurred: \" + err.Error())\n}\n\n// after\nif err != nil {\n    log.Fatalf(\"ERROR occurred: %v\", err) // clean exit, still loud\n}","handlingStrategy":"try-catch","validationCode":null,"typeGuard":null,"tryCatchPattern":"// keep the abort contained and observable\ndefer func() {\n    if r := recover(); r != nil {\n        log.Printf(\"recovered from fatal branch: %v\", r)\n        os.Exit(1)\n    }\n}()\nif err != nil {\n    panic(\"ERROR occurred: \" + err.Error())\n}","preventionTips":["Return errors and let main decide how to terminate; panic dumps skip caller cleanup","Use log.Fatal for startup-style fatal errors — same effect, cleaner output","Reserve panic for invariants a human must fix in code, not runtime conditions"],"tags":["go","error-handling","panic","exit","idiom"],"backgroundTag":null,"analyzedSha":"7a54d34d3657084b6a59e5618bd069b912d571aa","analyzedAt":"2026-08-15T15:13:06.026Z","schemaVersion":2},"datasetVersion":"2026-08-15T17:31:12.345Z"}