codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Follow publication

Making No Exceptions: Literally

Ittai Yam
codeburst
Published in
4 min readMay 31, 2018

package mainimport "fmt"
import "errors"
func divide(a int, b int) (error, int) {
if b == 0.0 {
return errors.New("Division by zero"), 0.0
} else {
return nil, (a/b);
}
}
func main() {
err1, result1 := divide(8,4)
if err1 == nil {
fmt.Printf("Result == %d.\n", result1)
} else {
fmt.Println(err1)
}
err2, result2 := divide(8,0)
if err2 == nil {
fmt.Printf("Result == %d.\n", result2)
} else {
fmt.Println(err2)
}
}
function findAndPrintUser(id) {
users.findById(id)
.then((user) => console.log(user.name))
.catch((err) => console.error(err));
}
async function findAndPrintUserAsync(id) {
try {
const user = await users.findById(id)
console.log(user.name);
} catch (err) {
console.error(err);
}
}
function to(promise) {
return promise
.then(data => [null, data])
.catch(err => [err, null])
}
async function findAndPrintUserAsync(id) {
const [err, user] = await to(users.findById(id))
if (err) {
console.error(err);
} else {
console.log(user.name);
}
}

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Published in codeburst

Bursts of code to power through your day. Web Development articles, tutorials, and news.

Written by Ittai Yam

A software developer, a problem solver and a miracle worker walk(s) into a bar and has/have Guinness.

No responses yet

Write a response