E4120

E4120#

The application might raise errors, but it’s not handled. Try adding an infix operator ! or ? to the application.

In MoonBit, we require programmers to explicitly annotate which functions may raise errors. This is done by adding an infix operator ! or ? to the function application. The ! operator to re-raise the error, and the ? operator is to materialize the error to a Result[T, E] type.

错误示例#

fn may_raise_error(input : Int) -> Unit! {
  if input == 42 {
    return
  }
  fail!("failed")
}

fn main {
  may_raise_error(42)
  // Error: The application might raise errors of type Error, but it's not handled.
  // Try adding a infix operator `!` or `?` to the application, so that it looks like `...!(...)` or `...?(...)`.
}

建议#

You can either re-raise the error:

fn main {
  try {
    may_raise_error!(42)
  } catch {
    error => println("Error: \{error}")
  }
}

Or materialize the error to a Result[T, E] type:

fn main {
  let result = may_raise_error?(42)
  println("Result: \{result}")
}