> For the complete documentation index, see [llms.txt](https://go.netdpi.net/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://go.netdpi.net/functions/panic.md).

# 07.07-Panic & Recover

我們之前已經建立了一個名為 `panic` 的函式，此函式會產生執行期（run-time）的錯誤。我們也能使用內建的 `recover`  函式來處理執行期的 panic。`recover` 會停止 panic 並將傳遞給  `panic` 呼叫的參數值傳回。我們可以透過如下的方式來使用它：

```go
package main

import "fmt"

func main() {
    panic("PANIC")
    str := recover()
    fmt.Println(str)
}
```

可是在這個案例中， `recover` 絕對不會執行到，因為在呼叫 `panic` 時已經暫停執行函式。所以我們必須搭配使用  `defer`：

```go
package main

import "fmt"

func main() {
    defer func() {    
        str := recover()
        fmt.Println(str)
    }()
    panic("PANIC")
}
```

通常一個 panic 代表的是一個程式設計師的錯誤（例如想要存取超出陣列邊界的索引值、忘記對一個 map 進行初始化之類），或是有難以還原的例外情況發生（所以才稱之為 panic）。


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://go.netdpi.net/functions/panic.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
