2019-02-02 16:18:25 +08:00
|
|
|
// Copyright 2017 gf Author(https://github.com/gogf/gf). All Rights Reserved.
|
2017-12-29 16:03:30 +08:00
|
|
|
//
|
|
|
|
// This Source Code Form is subject to the terms of the MIT License.
|
|
|
|
// If a copy of the MIT was not distributed with this file,
|
2019-02-02 16:18:25 +08:00
|
|
|
// You can obtain one at https://github.com/gogf/gf.
|
2017-12-29 16:03:30 +08:00
|
|
|
|
2019-05-08 17:21:18 +08:00
|
|
|
// Package gutil provides utility functions.
|
2017-11-23 10:21:28 +08:00
|
|
|
package gutil
|
|
|
|
|
2018-04-29 21:33:47 +08:00
|
|
|
import (
|
2019-07-29 21:01:19 +08:00
|
|
|
"github.com/gogf/gf/internal/empty"
|
2018-04-29 21:33:47 +08:00
|
|
|
)
|
|
|
|
|
2019-05-11 17:56:14 +08:00
|
|
|
// Throw throws out an exception, which can be caught be TryCatch or recover.
|
2018-11-06 18:53:25 +08:00
|
|
|
func Throw(exception interface{}) {
|
2019-06-19 09:06:52 +08:00
|
|
|
panic(exception)
|
2018-11-06 13:53:06 +08:00
|
|
|
}
|
|
|
|
|
2019-05-11 17:56:14 +08:00
|
|
|
// TryCatch implements try...catch... logistics.
|
2019-06-19 09:06:52 +08:00
|
|
|
func TryCatch(try func(), catch ...func(exception interface{})) {
|
|
|
|
if len(catch) > 0 {
|
|
|
|
// If <catch> is given, it's used to handle the exception.
|
|
|
|
defer func() {
|
|
|
|
if e := recover(); e != nil {
|
|
|
|
catch[0](e)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
} else {
|
|
|
|
// If no <catch> function passed, it filters the exception.
|
|
|
|
defer func() {
|
|
|
|
recover()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
try()
|
2018-11-06 13:53:06 +08:00
|
|
|
}
|
|
|
|
|
2019-05-11 17:56:14 +08:00
|
|
|
// IsEmpty checks given <value> empty or not.
|
|
|
|
// It returns false if <value> is: integer(0), bool(false), slice/map(len=0), nil;
|
|
|
|
// or else returns true.
|
2019-03-21 18:20:20 +08:00
|
|
|
func IsEmpty(value interface{}) bool {
|
2019-06-19 09:06:52 +08:00
|
|
|
return empty.IsEmpty(value)
|
2019-03-21 18:20:20 +08:00
|
|
|
}
|