GoCollections/exceptions/NPE.go

28 lines
475 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-23 10:15:18 +08:00
func NewNPE(message any, config *ExceptionConfig) *NPE {
2022-03-21 11:02:41 +08:00
return &NPE{
2021-05-21 11:14:07 +08:00
NewRuntimeException(
message,
"exception caused NullPointerException:",
2022-03-28 18:19:05 +08:00
config.AddSkipStack(1),
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)))
}
}