unknwon/the-way-to-go_ZH_CN · error

A severe error occurred: stopping the program!

Error message

A severe error occurred: stopping the program!

What it means

Deliberate teaching panic in example 13.2 (panic.go): main prints one line, then calls panic("A severe error occurred: stopping the program!"). It demonstrates panic semantics — the runtime prints the panic value with a stack trace, deferred functions run during unwinding, the program crashes, and the fmt.Println after the panic is unreachable.

Source

Thrown at eBook/13.2.md:14

# 13.2 运行时异常和 panic

当发生像数组下标越界或类型断言失败这样的运行错误时,Go 运行时会触发*运行时 panic*,伴随着程序的崩溃抛出一个 `runtime.Error` 接口类型的值。这个错误值有个 `RuntimeError()` 方法用于区别普通错误。

`panic()` 可以直接从代码初始化:当错误条件(我们所测试的代码)很严苛且不可恢复,程序不能继续运行时,可以使用 `panic()` 函数产生一个中止程序的运行时错误。`panic()` 接收一个做任意类型的参数,通常是字符串,在程序死亡时被打印出来。Go 运行时负责中止程序并给出调试信息。在示例 13.2 [panic.go](examples/chapter_13/panic.go) 中阐明了它的工作方式:

```go
package main

import "fmt"

func main() {
	fmt.Println("Starting the program")
	panic("A severe error occurred: stopping the program!")
	fmt.Println("Ending the program")
}
```

输出如下:

```
Starting the program
panic: A severe error occurred: stopping the program!
panic PC=0x4f3038
runtime.panic+0x99 /go/src/pkg/runtime/proc.c:1032
       runtime.panic(0x442938, 0x4f08e8)
main.main+0xa5 E:/Go/GoBoek/code examples/chapter 13/panic.go:8
       main.main()
runtime.mainstart+0xf 386/asm.s:84
       runtime.mainstart()
runtime.goexit /go/src/pkg/runtime/proc.c:148
       runtime.goexit()

View on GitHub (pinned to 7a54d34d36)

Solutions

  1. If hit while studying the book: this is the expected output — nothing is broken
  2. In real code, remove the panic and use normal control flow (return an error, os.Exit with a status)
  3. If aborting is genuinely intended, prefer log.Fatal for a clean single-line exit
  4. Run with 'go run panic.go' and read the unwind trace to learn the defer/panic interaction

Example fix

// before
fmt.Println("Starting the program")
panic("A severe error occurred: stopping the program!")
fmt.Println("Ending the program")

// after
fmt.Println("Starting the program")
if err := doWork(); err != nil {
    log.Fatalf("A severe error occurred: %v", err)
}
fmt.Println("Ending the program")
Defensive patterns

Strategy: try-catch

Try / catch

// capture the demo panic instead of crashing
defer func() {
    if r := recover(); r != nil {
        fmt.Println("recovered from demo panic:", r)
    }
}()
fmt.Println("Starting the program")
panic("A severe error occurred: stopping the program!")

Prevention

When it happens

Trigger: Unconditional: every execution of the demo binary reaches the panic call after the first Println. No environment, input, or configuration triggers it — it is the lesson itself.

Common situations: New Go developers running the eBook example to observe panic output; copy-pasting the demo skeleton into real code and forgetting to remove the panic; expecting 'Ending the program' to print (it never will).

Related errors


AI-assisted analysis of unknwon/the-way-to-go_ZH_CN@7a54d34d36 (2026-08-15). Data as JSON: /api/errors/dbf224aa33d605b0. Report an issue: GitHub.