GoCollections/exceptions/NPE.go

24 lines
446 B
Go
Raw Normal View History

2021-05-21 11:14:07 +08:00
package exceptions
2022-03-28 18:19:05 +08:00
import (
"github.com/tursom/GoCollections/lang"
"reflect"
)
2021-05-21 11:14:07 +08:00
type NPE struct {
RuntimeException
}
2022-03-31 01:22:30 +08:00
func NewNPE(message string, config *ExceptionConfig) *NPE {
2022-03-21 11:02:41 +08:00
return &NPE{
2022-03-31 01:22:30 +08:00
NewRuntimeException(message, config.AddSkipStack(1).SetExceptionName("NPE")),
2021-05-21 11:14:07 +08:00
}
}
2022-03-28 18:19:05 +08:00
func CheckNil[T any](p *T) {
t := reflect.TypeOf(lang.Nil[T]())
if p == nil {
panic(NewNPE(t.Name()+" is null", DefaultExceptionConfig().AddSkipStack(1)))
}
}