Исходники и презентации
This commit is contained in:
42
lessons/sync_primitives/defer_with_panic/main.go
Normal file
42
lessons/sync_primitives/defer_with_panic/main.go
Normal file
@ -0,0 +1,42 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"log"
|
||||
"sync"
|
||||
)
|
||||
|
||||
var mutex sync.Mutex
|
||||
|
||||
func functionWithPanic() {
|
||||
panic("error")
|
||||
}
|
||||
|
||||
func handle1() {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Println("recovered")
|
||||
}
|
||||
}()
|
||||
|
||||
mutex.Lock()
|
||||
defer mutex.Unlock()
|
||||
|
||||
functionWithPanic()
|
||||
}
|
||||
|
||||
func handle2() {
|
||||
defer func() {
|
||||
if err := recover(); err != nil {
|
||||
log.Println("recovered")
|
||||
}
|
||||
}()
|
||||
|
||||
mutex.Lock()
|
||||
functionWithPanic()
|
||||
mutex.Unlock()
|
||||
}
|
||||
|
||||
func main() {
|
||||
handle1()
|
||||
handle2()
|
||||
}
|
||||
Reference in New Issue
Block a user