Open chuigda opened 3 years ago
[JUG] Does anyone have better solution than exceptions?
[Phosphorus] One of the weird point of such checking is: you have to declare an exception that logically cannot be thrown out.
if (false) {
throw new SomeException();
}
The case above is simple enough so it can be easily resolved via static analysis. Not all cases can be statically analyzed, however.
[SchrodingerZhu] I suggest using algebraic effects. Here's a tutorial. https://dev.to/yelouafi/algebraic-effects-in-javascript-part-1---continuations-and-control-transfer-3g88
[MID] It is also possible to introduce Result
type to Pr47. I'll create another proposal then.
[SchrodingerZhu] I suggest using algebraic effects. Here's a tutorial.
[MID] Functional programming is crap.
[Phosphorus] One of the weird point of such checking is: you have to declare an exception that logically cannot be thrown out.
if (false) { throw new SomeException(); }
The case above is simple enough so it can be easily resolved via static analysis. Not all cases can be statically analyzed, however.
[msk] This can be resolved with a catch
clause with an unreachable
.
[MID] Since it seems that there's no critical problem with this solution, and currently we don't have better solution than this, I think we can start standardizing exception handling.
[JUG] Here comes a design issue: should unchecked exceptions be recoverable?
If we'd like to simplify the problem, we can make unchecked exceptions non-recoverable, and we can write out simple and efficient implementation. As far as we know panic!
in Rust is also almost not recoverable.
[JUG] Here comes a design issue: should unchecked exceptions be recoverable?
[SUP] This is configurable, either at compile time or run time.
[T10] We've found that adding checked exceptions may add some overhead to runtime implementation, even if user does not use exception mechanism at all. We'll continue to focus on this.
[JUG] Here comes a design issue: should unchecked exceptions be recoverable?
[SUP] This is configurable, either at compile time or run time.
[T10] Making unchecked exception recoverable can significantly increase the complexity of static analysis.
[MID] The implementation team is on the treasure hunt: https://github.com/Pr47/Pr47/pull/15.
User-defined types cannot carry stack-trace or relevant information. I suggest adding an exc<T>
container type. This will somewhat increase the complexity, but fine.
[MID] Stack traces might be less useful. But fine, I'll take this into consideration.
Abstract
This proposal serves to add a Java-like structural and checked exception system to the Pr47 language, in order to enhance the error handling of this language.
Existing problems and workarounds
Handling error has been a problem of long history. Currently, it is able to handle error with return values and error codes. That looks like:
This way is too primitive to be the error handling mechanism of a modern scripting language. As everyone sees, golang has been criticized for so long for such error handling feature. What's more, Pr47 is intended to support seeming interaction with Rust, while most Rustaceans do not handle errors with error codes -- They use the
Result<T, E>
type.This proposal's method
By adding structural, checked exceptions into our language, allowing it to interact with Rust
Result
, we can consequentially have better error handling solution.User may use any self-defined type or imported type as exception type, and can create exceptions with
throw
, can catch and handle exceptions withtry
-catch
.Different exception types should be caught with different catch clauses with corresponding types. Catch clause with
any
type could catch exceptions of arbitrary type.Within a function, an exception must be either explicitly caught, or explicitly marked with exception specification. Marking an exception that will never be thrown is disallowed.
Two functions only differ on exception specifications does not constitute overloading.
For Rust functions that return
Result<T, E>
types, when fetching the return value of taht function, if theResult
contains anErr(E)
, the innerE
will be transformed into an Pr47 exception.Checked exceptions have been criticized for almost twenty years [1] [2]. However, I together with Yinwang [3], don't think so. The descedant languages (C# and Kotlin) who attacked checked exceptions are even unable to provide a mechanism of equivalent good.
Even with checked exceptions, user may use
throws(any)
to turn checked into unchecked. However, if the language provide unchecked exception by itself, there's no way to use it in a checked way within the language.What's more, it is possible to disable exception specification checking with attributes in Pr47, unlike Java. If a user finds the check useless, just disable it, and there won't be any problem.
Unresolved problems
See also