Re:Fourteen Years of Go
by zoky on Sunday November 12, 2023 @09:53AM
(#38240550)
That is a terrible example of a reason to panic. If the programmer believes the list to be exhaustive and it isn’t, then the programmer is wrong. So raise an exception and let the caller decide what to do with it. But don’t punish the caller by forcing it to crash just because you made a mistake as a programmer.
[ Parent
| on HN ]
(in go, "raise an exception" means "return an error.") error values are meant to represent expected unsuccessful conditions, internal properties of the program, during the program's execution. errors in go---and exceptions in other languages---are not meant to represent mistakes by the package author. in go these are panics. but perhaps other language reuse exceptions for both.
put another way: how is the non-exhaustiveness of the switch statement qualitatively different from an out-of-bounds array access? they both are equivalent package author mistakes.
the java runtime raises IndexOutOfBounds exceptions for bad array accesses, but do each function call or your program with an IndexOutOfBounds exception handler?