def error_func() raises -> Int: raise "something went wrong" def typed_func() raises ValidationError -> Int: raise ValidationError(field="x", reason="invalid") # This doesn't compile def mixed() raises ValidationError: try: _ = error_func() # raises Error _ = typed_func() # raises ValidationError except e: print(e) ``` The compiler reports: ```output error: cannot call function that may raise 'Error' in a context that supports an error type of 'ValidationError' ``` To call both functions, use separate `try` blocks or wrap the `Error`-raising function as shown in [Wrap `Error` at API boundaries](#wrap-error-at-api-boundaries). ### Recommendations for mixed codebases When working with both `Error` and typed errors: - **Use `raises YourErrorType`** — Always specify the error type in function signatures. Bare `raises` discards type information. - **Use separate `try` blocks** — When calling functions with different error types, use nested or sequential `try` blocks to handle each type independently. :::note For a complete working example of these interaction patterns, see [`error_interaction.mojo`](https://github.com/modular/modular/blob/mojo/v1.2.0/Mojo/docs/site/code/manual/errors/error_interaction.mojo). ::: ## Enable stack trace generation for errors Because Mojo represents errors as alternate return values rather than stack-unwinding exceptions, stack trace collection isn't automatic. Collecting a stack trace requires heap allocation and adds runtime overhead, so it's disabled by default to keep error handling lightweight. :::important Stack traces are a feature of the built-in `Error` type only. Typed errors currently don't capture stack traces because the trace is collected inside `Error.__init__()`, and custom error structs have no equivalent hook. The examples in this section all use `Error` intentionally. ::: Mojo generates a stack trace when your program hits a segmentation fault. However, by default Mojo *doesn't* generate a stack trace when your program raises an error—this avoids the additional runtime overhead. To enable stack traces for raised errors, set the `MODULAR_DEBUG` environment variable to `stack-trace-on-error`, as shown in the examples below. Keep in mind that when you compile your program with [`mojo build`](/docs/cli/build/), the compiler optimizes and strips symbols by default, so often your stack trace won't be very useful. Consider this program: ```mojo title="stacktrace_error.mojo" def func2() raises -> None: raise Error("Intentional error") def func1() raises -> None: func2() def main() raises: func1() ``` If you compile the program with default settings and run it with the environment variable set, you'll see a stack trace without symbols: ```sh mojo build stacktrace_error.mojo ``` ```sh MODULAR_DEBUG=stack-trace-on-error ./stacktrace_error