golang/README

57 lines
1.1 KiB
Plaintext

Eccezioni stile Java in GO.
- Blog panic & recover: https://go.dev/blog/defer-panic-and-recover
- Libreria completa: https://hackthology.com/exceptions-for-go-as-a-library.html
- Implementazione semplificata: https://hackthology.com/exceptions-for-go-as-a-library.html
package main
import (
"fmt"
)
type Block struct {
Try func()
Catch func(Exception)
Finally func()
}
type Exception interface{}
func Throw(up Exception) {
panic(up)
}
func (tcf Block) Do() {
if tcf.Finally != nil {
defer tcf.Finally()
}
if tcf.Catch != nil {
defer func() {
if r := recover(); r != nil {
tcf.Catch(r)
}
}()
}
tcf.Try()
}
func main() {
fmt.Println("application started")
Block {
Try: func() {
fmt.Println("this is good")
Throw("Oh,no...!!")
},
Catch: func(e Exception) {
fmt.Printf("Caught %v\n", e)
},
Finally: func() {
fmt.Println("finally keep runs")
},
}.Do()
fmt.Println("application shutdown")
}
----------------------------------------------