0% found this document useful (0 votes)
65 views13 pages

23.1 Error Handling

Errors are an important part of programming that must be handled. Functions can return either nil if there is no error, or a non-nil error value if a problem occurs. It is crucial to check for errors immediately after a function call using an if statement to see if the returned error value is nil or not nil, and quit if an error is present.

Uploaded by

kaleab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
65 views13 pages

23.1 Error Handling

Errors are an important part of programming that must be handled. Functions can return either nil if there is no error, or a non-nil error value if a problem occurs. It is crucial to check for errors immediately after a function call using an if statement to see if the returned error value is nil or not nil, and quit if an error is present.

Uploaded by

kaleab
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Why?

Not everything goes according to the plan


Things can fail
• Network problems
• File access problems
• User input problems
• ...
Error handling is crucial
And it is part of your programming job
nil
nil value means that the value is not initialized yet

javascript python java ruby


null None null nil
nil
zero value for pointer-based types

pointers slices maps interfaces channels


nil nil nil nil nil
nil
means there is no error

err := do()

if err != nil {
// ❌ error
// handle it!
// terminate!
}

// 👍 success
// continue...
ERROR VALUE
strconv.Itoa never fails

func Itoa(i int) string

✓ this function never returns errors


ERROR VALUE
strconv.Atoi sometimes fails, so you've to handle the error

func Atoi(s string) (int, error)

returns a value with an error type

✓ success nil
(uninitialized error value)
error non-nil
(initialized error value)
summary
nil
if err != nil {
// ❌ error
// quit
}
// 👍 success
error
if err != nil {
// ❌ error
// quit
}
// 👍 success
immediately handle the errors
if err != nil {
// ❌ error
// quit
}
// 👍 success
there is no throw/catch
if err != nil {
// ❌ error
// quit
}
// 👍 success

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy