I'm working on a new general-purpose programming language. Exception handling is supposed to be like in Rust, but simpler. I'm specially interested in feedback for exception handling (throw, catch).
Questions:
The
catch
catches all exceptions that were thrown within the scope. I argue there is no need fortry
, because try would requires (unnecessary, in my view) indentation, and messes up diffs. I wonder if other programming languages have this feature? I didn't find any. Is there a problem with this idea?I think one exception type is sufficient. It has the fields code (int), message (string), and optional data (payload - byte array). Do you see any obvious problem with this?
Exceptions
throw
throws an exception. catch
is needed, or the method needs throws
. In the following example, the scope of the catch
is the while
loop.
fun factorial(x int) int throws
if x > 20
throw exception('Value too large')
if x <= 1
return 1
f := factorial(x - 1)
return x * f
fun main()
i := 0
while i <= 30
println('Factorial of ' i ' is ' factorial(i))
catch e
println('Factorial of ' i ' resulted in ' e.message)
i += 1
e
is a variable name that contains the exception. Yes the exception could come from multiple locations (same as in other programming languages that have a try ... catch block). I think that is good: I wouldn't want that the programmer has to handle possible exceptions for each and every method call (as in C or Go). That is the point of using "catch". But unlike Java, in my language, each of the methods that can throw an exception needs to declare it. So my language is similar to Swift, in this regard. $\endgroup$