knit_script.knit_script_interpreter.statements.try_catch_statements module
Try catch execution.
This module provides the Try_Catch_Statement class, which implements exception handling control flow in knit script programs. It allows programs to gracefully handle errors and exceptions that may occur during knitting operations, providing robust error recovery mechanisms.
- class Try_Catch_Statement(parser_node, try_statement, catch_statement, errors)[source]
Bases:
Statement
Manages try-catch exception handling structure.
Executes a statement within a try block, and if specified exceptions occur, executes a catch block. Supports filtering by specific exception types and binding caught exceptions to variables for inspection and handling.
This statement provides robust error handling capabilities for knit script programs, allowing developers to anticipate and handle potential issues like machine configuration errors, invalid operations, or resource constraints. The try-catch mechanism ensures that programs can recover gracefully from errors rather than terminating unexpectedly.
- _errors
List of expressions that evaluate to exception types to catch.
- Type:
- Parameters:
parser_node (
LRStackNode
)try_statement (
Statement
)catch_statement (
Statement
)errors (
list
[Expression
])
- __init__(parser_node, try_statement, catch_statement, errors)[source]
Initialize a try-catch statement.
- Parameters:
parser_node (LRStackNode) – The parser node from the abstract syntax tree.
try_statement (Statement) – The statement to execute within the try block.
catch_statement (Statement) – The statement to execute if a matching exception occurs.
errors (list[Expression]) – List of expressions that evaluate to exception types to catch.
empty (Can include Assignment expressions to bind the exception to a variable. If)
exceptions. (catches all)
- execute(context)[source]
Execute the try-catch block using Python’s exception handling.
Attempts to execute the try statement. If an exception occurs and matches one of the specified error types (or if no types are specified), executes the catch statement. If the error expression is an Assignment, binds the exception to the specified variable name in a new scope.
- Parameters:
context (Knit_Script_Context) – The current execution context of the knit script interpreter.
- Return type:
- __str__()[source]
Return string representation of the try-catch statement.
- Returns:
A string showing the try statement, error types, and catch statement.
- Return type:
- __repr__()[source]
Return detailed string representation of the try-catch statement.
- Returns:
Same as __str__ for this class.
- Return type:
- property line_number: int
Get the line number of the symbol that generated this statement.
- Returns:
The line number where this element appears in the source file.
- Return type:
- property location: Location
Get the location of this symbol in KnitScript file.
- Returns:
The location of this symbol in the source file, including file name, line number, and position information.
- Return type:
Location