Skip to main content
Monty provides a hierarchy of exception classes for different types of errors that can occur during code parsing, type checking, and execution.

Exception Hierarchy

MontyError

Base exception for all Monty interpreter errors. Catching MontyError will catch syntax, runtime, and typing errors from Monty. This exception is raised internally by Monty and cannot be constructed directly.

Methods

exception()

Returns the inner exception as a Python exception object.
The underlying Python exception

__str__()

Returns the exception message.
The exception message string

Example

MontySyntaxError

Raised when Python code has syntax errors or cannot be parsed by Monty. Inherits exception() and __str__() from MontyError.

Methods

display()

Returns formatted exception string.
Literal['type-msg', 'msg']
default:"'msg'"
  • 'type-msg': Returns 'ExceptionType: message' format
  • 'msg': Returns just the message
Formatted error message

Example

MontyTypingError

Raised when type checking finds errors in the code. This exception is raised when static type analysis detects type errors before execution. Inherits exception() and __str__() from MontyError. Cannot be constructed directly from Python.

Methods

display()

Renders the type error diagnostics with the specified format and color.
Literal['full', 'concise', 'azure', 'json', 'jsonlines', 'rdjson', 'pylint', 'gitlab', 'github']
default:"'full'"
Output format for the diagnostics:
  • 'full': Complete diagnostic output with context
  • 'concise': Abbreviated diagnostic output
  • 'azure': Azure Pipelines format
  • 'json': JSON format
  • 'jsonlines': JSON Lines format
  • 'rdjson': Reviewdog JSON format
  • 'pylint': Pylint format
  • 'gitlab': GitLab CI format
  • 'github': GitHub Actions format
bool
default:"False"
Whether to include ANSI color codes in the output
Formatted type error diagnostics

Example

MontyRuntimeError

Raised when Monty code fails during execution. Inherits exception() and __str__() from MontyError. Additionally provides traceback() and display() methods.

Methods

traceback()

Returns the Monty traceback as a list of Frame objects.
List of stack frames showing the call stack at the time of the error

display()

Returns formatted exception string.
Literal['traceback', 'type-msg', 'msg']
default:"'traceback'"
  • 'traceback': Full traceback with exception
  • 'type-msg': 'ExceptionType: message' format
  • 'msg': Just the message
Formatted error output

Example

Frame

A single frame in a Monty traceback. This class provides detailed information about a specific point in the call stack.

Properties

str
The filename where the code is located
int
Line number (1-based)
int
Column number (1-based)
int
End line number (1-based)
int
End column number (1-based)
str | None
The name of the function, or None for module-level code
str | None
The source code line for preview in the traceback

Methods

dict()

Returns a dictionary of all frame attributes.
Dictionary containing all frame properties

Example

Error Handling Best Practices

Catch Specific Exceptions

Handle Resource Limit Errors

Extract Detailed Information