Try/catch or try/except?

6 views
Skip to first unread message

Bill Cox

unread,
May 4, 2023, 8:17:51 PM5/4/23
to Rune language discussion
Python uses try/except, which I find annoying, but maybe it's just me.

We have a proof-of-concept try/catch syntax just to get something going in the C Rune compiler, and probably won't bother getting it working properly, because it can wait for the bootstrap.  However, it would be nice to support the expected syntax that the bootstrap compiler will used.  Right now, we just have:

try {
    throw "No!!!"
} catch e {
    println e
}

That's not going to be good enough, probably not even for the bootstrap.  How about something like:

enum StatusCode : u8 {
  Ok = 0,
  Cancelled = 1,
  Unknown = 2,
  InvalidArgument = 3,
  ...
}
try {
  handlePpc(rpc)
} catch (StatusCode.Unknown, msg) {
  ...
} catch (status: StatusCode, msg) {
  ...
} catch e {
  println "Unhandled exception: ", e
  throw e
}

This syntax would introduce "patterns" into Rune, where we can explode tuples more easily, like Python.  It would allow:



(a, b) = (b, a)

Which is how we swap 2 variables in Python.  It would also allow:

switch point {
  (x, y) {
    println x, ", ", y
  }
}

I know we need this, but have put it off, hoping to build it in the bootstrap.  There isn't much exception handling required in a compiler.

Andrew Wilson

unread,
May 4, 2023, 8:32:57 PM5/4/23
to Bill Cox, Rune language discussion
I don't mind Python's try/raise/except.  If we want to appeal to python folks, it seems better to adopt similar syntax for similar semantics.

Why not push the boat out and do something crazy like, 'expect/alas/otherwise'?

expect {
  alas 'No'
} otherwise 'No' => <something> {
} ...

I am a big fan of pattern matching; but if you are really averse to writing it in the C bootstrap, you could always do if/then/else matches inside the body of the catch/except/otherwise clauses:

expect {
  alas <something>
} otherwise e {
   if (e == ...) then ...
  else if (e == ...) then ... 
 ...
}


--
You received this message because you are subscribed to the Google Groups "Rune language discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rune-discuss...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/rune-discuss/CAH9QtQF0u-f_UvjzOo2eJcWB0%3DwxmctBSwAmsqC4HQyCUqv1wA%40mail.gmail.com.
For more options, visit https://groups.google.com/d/optout.


--
Andrew Wilson
Software Engineer, Android TV Eng Prod

Bill Cox

unread,
May 4, 2023, 8:33:19 PM5/4/23
to Rune language discussion
Maybe use syntax more like the switch statement?

try {
   ...
} catch {
  (StatusCode.Unknown, msg: string) => panic "We don't allow unknown exceptions here"
  (status: Status, msg: string) => throw status, msg
}

Andrew Wilson

unread,
May 4, 2023, 8:34:43 PM5/4/23
to Bill Cox, Rune language discussion
shall we vote on the preferred nomenclature?  try/throw/catch  vs try/raise/except vs expect/alas/otherwise?

--
You received this message because you are subscribed to the Google Groups "Rune language discussion" group.
To unsubscribe from this group and stop receiving emails from it, send an email to rune-discuss...@googlegroups.com.

For more options, visit https://groups.google.com/d/optout.

Andrew Wilson

unread,
May 4, 2023, 8:35:47 PM5/4/23
to Bill Cox, Rune language discussion
oh, and I think that the switch statement syntax in the body of the exception handler clause looks much better.

Bill Cox

unread,
May 4, 2023, 8:39:49 PM5/4/23
to Andrew Wilson, Rune language discussion
What do you think of Python's else feature to handle the case where nothing is thrown?  I've never used it and never felt the need.

Andrew Wilson

unread,
May 4, 2023, 9:25:26 PM5/4/23
to Bill Cox, Rune language discussion
I've never used 'else' in a try/except block. though I have used 'finally'
Reply all
Reply to author
Forward
0 new messages