Case in point from a popular framework, Django: the standard way of getting an object from the DB via the ORM is Class.objects.get. This method either returns a single object or raises on exception if there are zero rows in the db or another exception if there are two or more rows. It may also raise other kinds of exceptions.
Now, it's clear that having zero rows is not very exceptional, and even >1 is somewhat debatable. Note especially that this is not just some weekend project by a nobody, it is a framework that is widely used and respected.
I can't agree about Class.objects.get - method must return exactly one row by known pk value.
So it perfectly sane to raise exception for zero rows.
Class.objects.filter is working the way you want.
I have every reason to believe that the Django devs are capable people who thought about this and landed on this specific usage with a clear rationale... but it still is a misuse of error handling capabilities of a language. And a sort of misuse that everyone else does sometimes as well.
It's seemingly nearly mandatory in discussions regarding exceptions to bring up Common Lisps's condition system, which is a superset of exceptions. I've included a link to a chapter from a book on Lisp.
http://www.gigamonkeys.com/book/beyond-exception-handling-co...
func MaybeAnInt() (int, error) {
if FAIL {
return 0, errors.New("this is the error message")
}
return 42, nil
}
x, err := MaybeAnInt()
if err != nil {
// handle your error
}
// normal shit here.You now have to mix up your exception code and your normal code (or, as I suspect most people will do, ignore the error return value).
Did they kill stack traces too?
If you would think a bit more broadly, you might also see this so that all the error handling code is visible, explicit and right there with the actual logic, and not hidden away what might be (and often is) multiple layers of modules.
that's not true. You can ignore the second parameter by explicitly naming it with an underscore, like this:
x, _ := MightFail()>Did they kill stack traces too?
no, we have stack traces.
Also, it's Go, not GO.