mirror of
https://github.com/tursom/GoCollections.git
synced 2025-04-26 13:20:22 +08:00
add NPE
This commit is contained in:
parent
0fca6a7ca3
commit
a30503aa6a
@ -1,5 +1,7 @@
|
||||
package collections
|
||||
|
||||
import "github.com/tursom/GoCollections/exceptions"
|
||||
|
||||
type Iterator interface {
|
||||
HasNext() bool
|
||||
Next() (interface{}, error)
|
||||
@ -22,7 +24,7 @@ type MutableIterable interface {
|
||||
|
||||
func Loop(iterable Iterable, f func(element interface{}) error) error {
|
||||
if f == nil {
|
||||
return nil
|
||||
return exceptions.NewNPE("", true)
|
||||
}
|
||||
iterator := iterable.Iterator()
|
||||
for iterator.HasNext() {
|
||||
|
@ -6,6 +6,11 @@ type ElementNotFoundException struct {
|
||||
|
||||
func NewElementNotFoundException(message string, getStackTrace bool) *ElementNotFoundException {
|
||||
return &ElementNotFoundException{
|
||||
NewRuntimeException(message, "exception caused ElementNotFoundException:", getStackTrace),
|
||||
NewRuntimeException(
|
||||
message,
|
||||
"exception caused ElementNotFoundException:",
|
||||
getStackTrace,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
@ -3,29 +3,32 @@ package exceptions
|
||||
import (
|
||||
"fmt"
|
||||
"io"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type Exception interface {
|
||||
Cause() Exception
|
||||
Error() string
|
||||
StackTrace() []StackTrace
|
||||
PrintStackTrace()
|
||||
PrintStackTraceTo(writer io.Writer)
|
||||
BuildPrintStackTrace(builder *strings.Builder)
|
||||
}
|
||||
|
||||
func PrintStackTrace(writer io.Writer, e Exception, exceptionMsg string) {
|
||||
_, err := fmt.Fprintln(writer, exceptionMsg, e.Error())
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
func PrintStackTrace(builder *strings.Builder, e Exception, exceptionMsg string) {
|
||||
builder.WriteString(fmt.Sprintln(exceptionMsg, e.Error()))
|
||||
if e.StackTrace() == nil {
|
||||
return
|
||||
}
|
||||
for _, stackTrace := range e.StackTrace() {
|
||||
stackTrace.PrintLn(writer)
|
||||
stackTrace.WriteTo(builder)
|
||||
}
|
||||
}
|
||||
|
||||
func Try(f func() (ret interface{}, err error), catch func(interface{}) (ret interface{}, err error)) (ret interface{}, err error) {
|
||||
func Try(
|
||||
f func() (ret interface{}, err error),
|
||||
catch func(panic interface{}) (ret interface{}, err error),
|
||||
) (ret interface{}, err error) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
ret, err = catch(r)
|
||||
|
@ -6,6 +6,11 @@ type IndexOutOfBound struct {
|
||||
|
||||
func NewIndexOutOfBound(message string, getStackTrace bool) IndexOutOfBound {
|
||||
return IndexOutOfBound{
|
||||
NewRuntimeException(message, "exception caused IndexOutOfBound:", getStackTrace),
|
||||
NewRuntimeException(
|
||||
message,
|
||||
"exception caused IndexOutOfBound:",
|
||||
getStackTrace,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
16
exceptions/NPE.go
Normal file
16
exceptions/NPE.go
Normal file
@ -0,0 +1,16 @@
|
||||
package exceptions
|
||||
|
||||
type NPE struct {
|
||||
RuntimeException
|
||||
}
|
||||
|
||||
func NewNPE(message string, getStackTrace bool) *NPE {
|
||||
return &NPE{
|
||||
NewRuntimeException(
|
||||
message,
|
||||
"exception caused NullPointerException:",
|
||||
getStackTrace,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
}
|
@ -6,6 +6,11 @@ type OperationNotSupportedException struct {
|
||||
|
||||
func NewOperationNotSupportedException(message string, getStackTrace bool) OperationNotSupportedException {
|
||||
return OperationNotSupportedException{
|
||||
NewRuntimeException(message, "exception caused OperationNotSupportedException:", getStackTrace),
|
||||
NewRuntimeException(
|
||||
message,
|
||||
"exception caused OperationNotSupportedException:",
|
||||
getStackTrace,
|
||||
nil,
|
||||
),
|
||||
}
|
||||
}
|
||||
|
@ -3,15 +3,17 @@ package exceptions
|
||||
import (
|
||||
"io"
|
||||
"os"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type RuntimeException struct {
|
||||
message string
|
||||
exceptionMessage string
|
||||
stackTrace []StackTrace
|
||||
cause Exception
|
||||
}
|
||||
|
||||
func NewRuntimeException(message, exceptionMessage string, getStackTrace bool) RuntimeException {
|
||||
func NewRuntimeException(message, exceptionMessage string, getStackTrace bool, cause Exception) RuntimeException {
|
||||
var stackTrace []StackTrace = nil
|
||||
if getStackTrace {
|
||||
stackTrace = GetStackTrace()
|
||||
@ -25,9 +27,14 @@ func NewRuntimeException(message, exceptionMessage string, getStackTrace bool) R
|
||||
message: message,
|
||||
exceptionMessage: exceptionMessage,
|
||||
stackTrace: stackTrace,
|
||||
cause: cause,
|
||||
}
|
||||
}
|
||||
|
||||
func (o RuntimeException) Cause() Exception {
|
||||
return o.cause
|
||||
}
|
||||
|
||||
func (o RuntimeException) Error() string {
|
||||
if len(o.message) == 0 {
|
||||
return "index out of bound"
|
||||
@ -45,5 +52,23 @@ func (o RuntimeException) PrintStackTrace() {
|
||||
}
|
||||
|
||||
func (o RuntimeException) PrintStackTraceTo(writer io.Writer) {
|
||||
PrintStackTrace(writer, o, o.exceptionMessage)
|
||||
builder := strings.Builder{}
|
||||
o.BuildPrintStackTrace(&builder)
|
||||
bytes := []byte(builder.String())
|
||||
writeBytes := 0
|
||||
for writeBytes < len(bytes) {
|
||||
write, err := writer.Write(bytes[writeBytes:])
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
writeBytes += write
|
||||
}
|
||||
}
|
||||
|
||||
func (o RuntimeException) BuildPrintStackTrace(builder *strings.Builder) {
|
||||
PrintStackTrace(builder, o, o.exceptionMessage)
|
||||
if o.cause != nil {
|
||||
builder.WriteString("caused by: ")
|
||||
o.cause.BuildPrintStackTrace(builder)
|
||||
}
|
||||
}
|
||||
|
@ -4,6 +4,7 @@ import (
|
||||
"fmt"
|
||||
"io"
|
||||
"runtime"
|
||||
"strings"
|
||||
)
|
||||
|
||||
type StackTrace struct {
|
||||
@ -38,6 +39,10 @@ func (s StackTrace) PrintLn(writer io.Writer) {
|
||||
}
|
||||
}
|
||||
|
||||
func (s StackTrace) WriteTo(builder *strings.Builder) {
|
||||
builder.WriteString(fmt.Sprintf("\tat %s(%d)\n", s.file, s.line))
|
||||
}
|
||||
|
||||
func GetStackTrace() []StackTrace {
|
||||
stackTraceMax := 16
|
||||
stackTraceUsed := 0
|
||||
|
8
main.go
8
main.go
@ -13,7 +13,13 @@ func main() {
|
||||
fmt.Println("recover from panic", r)
|
||||
return nil, exceptions.NewIndexOutOfBound(fmt.Sprint(r), true)
|
||||
})
|
||||
err.(exceptions.Exception).PrintStackTrace()
|
||||
exception := err.(exceptions.Exception)
|
||||
exceptions.NewRuntimeException(
|
||||
exception.Error(),
|
||||
"test exception:",
|
||||
true,
|
||||
exception,
|
||||
).PrintStackTrace()
|
||||
|
||||
list := collections.NewArrayList()
|
||||
fmt.Println(list)
|
||||
|
Loading…
Reference in New Issue
Block a user