Contributed by Lorenzobattistelaopen in new window

Error

The Error class is used to handle errors in Mojo.

init

We are able to initialize empty errors, with custom messages and even with string references.

var err : Error = Error()
raise err
warning: Expression [3]:22:5: unreachable code after raise statement
    return
    ^



Error: 
var custom_err : Error = Error("my custom error")
raise custom_err
warning: Expression [4]:22:5: unreachable code after raise statement
    return
    ^



Error: my custom error
var ref : StringRef = StringRef("hello")
var err : Error = Error(ref)

raise err
warning: Expression [7]:24:5: unreachable code after raise statement
    return
    ^



Error: hello

fields

  • value: The error message.
var err : Error = Error("something is wrong")
print(err.value)
something is wrong

copyinit

Allows error to be copied.

var err : Error = Error("hey")
var other : Error = err

raise other
warning: Expression [8]:25:5: unreachable code after raise statement
    return
    ^



Error: hey