28 ms·
The general answer is "represent it in the type system". That's typically via discriminated unions like Rust's Result, or something which emulates it (such as
by symbol-mason 2y ago
The general answer is "represent it in the type system". That's typically via discriminated unions like Rust's Result, or something which emulates it (such as OneOf or Dunet for C#, although it will be a native part of the language soon).
Pattern matching, nullable types, and the Try* method conventions are also ways of representing the potential for failure explicitly.
- vips7L 2y agoChecked exceptions are represented in the type system. There is fundamentally no difference between these two: A b() throws C fn b() -> Result<A, C> A a = try { b() } catch (C c) { new A() } val a = b() match { Ok(a) => a Failure(c) => A() }