Skip to main content
Monty provides comprehensive error handling with detailed tracebacks, exception type preservation, and multiple error display formats.

Error Types

Monty has three main error types, all subclasses of MontyError:

MontySyntaxError

Raised when code has syntax errors during parsing:

MontyRuntimeError

Raised when code encounters a runtime error during execution:

MontyTypingError

Raised when type checking detects type errors:
All Monty errors inherit from MontyError, so you can catch all Monty-specific errors with a single except clause.

Catching All Monty Errors

Getting the Inner Exception

For MontySyntaxError and MontyRuntimeError, you can access the underlying Python exception:

Common Runtime Exceptions

Monty preserves standard Python exception types:

Tracebacks

Monty provides full traceback information for runtime errors:
Output:

Accessing Traceback Frames

You can access individual traceback frames programmatically:

Frame Properties

Each frame has these properties:
You can also get frame data as a dict:

Display Formats

Control how errors are displayed:

Full Traceback (Default)

Type and Message

Message Only

str(error) returns the same as error.display('type-msg') for easy printing.

JavaScript Error Handling

Exception Handling Within Monty Code

Monty code can catch and handle exceptions using try/except:

Exception Hierarchies

Monty respects Python exception hierarchies:
Supported exception hierarchies:
  • ArithmeticErrorZeroDivisionError, OverflowError
  • LookupErrorKeyError, IndexError
  • RuntimeErrorNotImplementedError, RecursionError

Exceptions from External Functions

Exceptions raised by external functions propagate to Monty code:
Exception types are preserved across the boundary. A ValueError raised by an external function is a ValueError in Monty code.

Uncaught External Exceptions

If Monty code doesn’t catch an exception from an external function, it propagates to the host:

Exceptions During Iterative Execution

You can resume execution with an exception:

Uncaught Exception in Resume

Best Practices

1

Catch specific errors first

Handle specific errors before catching general MontyError:
2

Log tracebacks for debugging

Always log the full traceback for runtime errors:
3

Provide helpful error messages

When raising exceptions from external functions, provide clear messages:
4

Handle errors in Monty code when possible

Encourage your agent to use try/except in generated code:
Always catch MontyError or its subclasses. Don’t let Monty exceptions crash your application.

Error Messages for LLMs

When providing error feedback to LLMs, use concise formats:

Testing Error Handling

Test that your code handles errors correctly:

Repr for Debugging

All error types have helpful repr() implementations:

Next Steps

Type Checking

Catch errors before runtime with type checking

External Functions

Learn how external function errors propagate