IIRC Mojo doesn’t handle errors like either C++ or Python. It secretly handles them like values.
Consider this code:
@no_inline
fn foo(i: Int) raises -> String:
var d: Dict[Int, String] = {1: 'hello', 2: 'world'}
return d[i]
fn main() raises:
var a = foo(10)
print(a)
Here in the IR you see that foo
doesn’t actually just return a string, but also an optional error value, and does a branch based on whether an error occurred or not
define internal { [2 x i64], i8 } @"playground::main()"() #0 {
...
%4 = call { i1, { ptr, i64 }, { ptr, i64, i64 } } @"playground::foo(::Int)"(i64 10)
%5 = extractvalue { i1, { ptr, i64 }, { ptr, i64, i64 } } %4, 0
%6 = extractvalue { i1, { ptr, i64 }, { ptr, i64, i64 } } %4, 2
store { ptr, i64, i64 } %6, ptr %1, align 8
%7 = extractvalue { i1, { ptr, i64 }, { ptr, i64, i64 } } %4, 1, 0
...
; exit normally or print error
br i1 %5, label %20, label %21
From a performance perspective. I don’t think this would be much different than Rust, but still uses try/catch
semantics like C++ and Python