E4120

E4120#

Compiler diagnostic name: unhandled_error.

The application might raise errors, but they are not handled.

Current MoonBit reports the ordinary unhandled re-raise pattern as E4122. This page is retained for older compiler output that used E4120 with the deprecated postfix error-handling syntax.

In MoonBit, we require programmers to explicitly annotate which functions may raise errors. A function that re-raises an error must use a raise annotation in its signature. To keep the current function from raising, handle the error with try ... catch or materialize it to a Result[T, E] value with try?.

Erroneous example#

See E4122 for checked erroneous examples with the current syntax.

Suggestion#

You can either annotate the caller so it re-raises the error:

///|
pub suberror ExampleError {
  Failed
}

///|
pub fn may_raise_error(input : Int) -> Unit raise ExampleError {
  if input == 42 {
    return
  }
  raise Failed
}

///|
pub fn caller() -> Unit raise ExampleError {
  may_raise_error(42)
}

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

///|
pub fn materialize_error() -> Result[Unit, ExampleError] {
  try? may_raise_error(42)
}