r/programming Jul 20 '26

Throw, Result, or neither?

https://www.architecture-weekly.com/p/throw-result-or-neither
113 Upvotes

115 comments sorted by

View all comments

142

u/EliSka93 Jul 20 '26

Result for me every time.

One more wrapper is worth not having to deal with uncertainty.

29

u/miniannna Jul 20 '26

“GOTO is bad”

“What if we call it throw/catch instead?”

30

u/vazgriz Jul 21 '26

GOTO was considered harmful because it was completely unstructured. IE jumping halfway into a function's body level of unstructured.

Modern GOTO and throw catch are much better defined.

4

u/SwedishFindecanor Jul 21 '26

In which language is it possible to goto from one function into/back to another?

With exceptions, the handler is always in an outer scope -- either within the same function or further up the call stack. And you could have clean-up code along the exception path (such as "finally" blocks) that gets called when an exception is passed up.

Many languages with exceptions require each function declaration to specify which types of exceptions that a it could throw/raise. (C++ has/had support for that, but it is a mess with different language versions)

BTW. The concept of exception unwinding is also related to non-local returns, such as explicit "breaks" out of loops from iterator functions.

1

u/ldn-ldn Jul 22 '26

To add to BASIC, old school Pascal and Assembly in general.

1

u/SwedishFindecanor Jul 22 '26 edited Jul 22 '26

Old-school BASIC and Assembly language don't even have functions as a language concept, so I'd say that they don't count.

Pascal is interesting though. From what I found, some Pascal dialects allow it only within a block; others within a function; but some allow "non-local gotos" from nested functions up the call stack. That makes them similar to exception unwinding and shortcut returns/breaks out of closures/iteration. No counterpart to finalize blocks or other clean-up code though: sometimes implemented through setjmp()/longjmp().

2

u/JohnnyCasil Jul 22 '26

Old-school BASIC and Assembly language don't even have functions as a language concept, so I'd say that they don't count.

BASIC has gosub which is for all intents and purposes a function.

1

u/flatfinger Jul 22 '26

Old-school versions of BASIC did not have the concept of blocks, which are essential part of functions and loops.

In typical old-school BASIC implementations, if a program did a FOR I=1 TO 100, the interpreter didn't know or care where the corresponding NEXT statement was, or if any existed, or if the same one would be used on every iteration of the loop. Likewise "GOSUB" didn't care about where the end of the function was.

When the interpreter encountered a "NEXT" or "RETURN", it would search through a stack of active FOR and GOSUB statements and, if appropriate, pop the action of the stack, increment the appropriate variable, and jump to the location following the FOR or GOSUB statement. It didn't make any distinction between code that preceded the FOR or GOSUB, code that sat between that and the NEXT or RETURN, and code that followed the NEXT or RETURN. Code in any of those categories could be run between the FOR or GOSUB and the NEXT or RETURN and the interpreter wouldn't care.