Does Go have exceptions?

Technology CommunityCategory: GolangDoes Go have exceptions?
VietMX Staff asked 3 years ago

No, Go takes a different approach. For plain error handling, Go’s multi-value returns make it easy to report an error without overloading the return value. Go code uses error values to indicate an abnormal state.

Consider:

func Open(name string) (file *File, err error)
f, err := os.Open("filename.ext")
if err != nil {
    log.Fatal(err)
}
// do something with the open *File f