Go language error Handling small example

Source: Internet
Author: User
This is a creation in Article, where the information may have evolved or changed.

Fullscreen's error handling can be a tragedy, and it is not conducive to differentiating between errors.
It is recommended that you use custom errors in your project, and then focus on the errors.

Package Main//error Processing Mode demo//author:xiong Chuan liang//date:2015-2-26import "FMT" Import "Errors" Func main () {Errtype ( Test0 ()) Errtype (Test1 ("Test1")) Errtype (Test2 ()) Errtype (Test3 ("Test3")) Errtype (Test4 ("test4"))}type Error1 struct {arg interrmsg string}func (e *error1) Error () string {return FMT. Sprintf ("%s", e.errmsg)}type Error2 struct {arg stringerrmsg string}func (e *error2) Error () string {return FMT. Sprintf ("%s", e.errmsg)}func test0 () error {return errors. New ("errors. New ()-test0 () ")}func test1 (arg string) error {return FMT. Errorf ("FMT. Errorf ()-test1 () ")}func test2 (arg int) *error1 {return &error1{arg," error1{}-Test2 () "}}func test3 (Arg string) err or {return &error2{arg, "error2{}-Test3 ()"}}func test4 (Arg string) *error2 {return &error2{arg, "error2{}-Test 4 () "}}func Errtype (Err interface{}) {switch e: = Err. ( Type) {case *error1:fmt. Println ("Type:error1", e) Case *error2:fmt. Println ("Type:error2", e) Case error:fmt. Println ("Type:error", E) DefaUlt:fmt. Println ("Type:default", e)}}/* operation effect: Type:error errors. New ()-test0 () Type:error FMT. Errorf ()-test1 () Type:error1 error1{}-test2 () Type:error2 error2{}-test3 () Type:error2 error2{}-test4 () */



Mail:xcl_168@aliyun.com

blog:http://blog.csdn.net/xcl168


Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.