Merge commit 'bed93e7d13cc18ebe3d5e146b92ac738f7c106fc' into ja
This commit is contained in:
@@ -59,9 +59,9 @@ In the following example, we pass a negative number to our `Sqrt` function. Chec
|
||||
fmt.Println(err)
|
||||
}
|
||||
|
||||
## Custom Error
|
||||
## Custom Errors
|
||||
|
||||
Through the above description we know that error is an interface, so in the realization of their package, by defining the structure implements this interface , we can realize their error definitions , see the package from Json Example:
|
||||
Through the above description, we know that a go Error is an interface. By defining a struct that implements this interface, we can implement their error definitions. Here's an example from the JSON package:
|
||||
|
||||
type SyntaxError struct {
|
||||
msg string // error description
|
||||
@@ -70,7 +70,7 @@ Through the above description we know that error is an interface, so in the real
|
||||
|
||||
func (e * SyntaxError) Error() string {return e.msg}
|
||||
|
||||
Error Offset field in the call time will not be printed , but we can get through a type assertion error type , then you can print an appropriate error message , see the following examples:
|
||||
The error's `Offset` field will not be printed at runtime when syntax errors occur, but using a type assertion error type, you can print the desired error message:
|
||||
|
||||
if err := dec.Decode(&val); err != nil {
|
||||
if serr, ok := err.(*json.SyntaxError); ok {
|
||||
@@ -80,20 +80,20 @@ Error Offset field in the call time will not be printed , but we can get through
|
||||
return err
|
||||
}
|
||||
|
||||
Note that, the function returns a custom error, the return value is set to recommend error type, rather than a custom error types, particular note should not be pre-declare custom error types of variables. For example:
|
||||
It should be noted that when the function returns a custom error, the return value is set to the recommend type of error rather than a custom error type. Be careful not to pre-declare variables of custom error types. For example:
|
||||
|
||||
func Decode() *SyntaxError {
|
||||
// error , which may lead to the upper caller err! = nil judgment is always true.
|
||||
var err * SyntaxError // pre- declare error variable
|
||||
// error, which may lead to the caller's err != nil comparison to always be true.
|
||||
var err * SyntaxError // pre-declare error variable
|
||||
if an error condition {
|
||||
err = & SyntaxError {}
|
||||
err = &SyntaxError{}
|
||||
}
|
||||
return err // error , err always equal non- nil, causes the upper caller err! = nil judgment is always true
|
||||
return err // error, err always equal non-nil, causes caller's err != nil comparison to always be true
|
||||
}
|
||||
|
||||
Cause see http://golang.org/doc/faq#nil_error
|
||||
See http://golang.org/doc/faq#nil_error for an in depth explanation
|
||||
|
||||
The above example shows how a simple custom Error type. But if we need more sophisticated error handling it? At this point, we have to refer to net package approach:
|
||||
The above example shows how to implement a simple custom Error type. But what if we need more sophisticated error handling? In this case, we have to refer to the `net` package approach:
|
||||
|
||||
package net
|
||||
|
||||
@@ -104,7 +104,7 @@ The above example shows how a simple custom Error type. But if we need more soph
|
||||
}
|
||||
|
||||
|
||||
Place the call through the type assertion err is not net.Error, to refine error handling , such as the following example , if a temporary error occurs on the network , it will sleep 1 seconds Retry :
|
||||
Using type assertion, we can check whether or not our error is of type net.Error, as shown in the following example. This allows us to refine our error handling -if a temporary error occurs on the network, it will sleep for 1 second, then retry the operation.
|
||||
|
||||
if nerr, ok := err.(net.Error); ok && nerr.Temporary() {
|
||||
time.Sleep(1e9)
|
||||
|
||||
Reference in New Issue
Block a user