Try#

class astroid.nodes.Try(*, lineno: int, col_offset: int, end_lineno: int, end_col_offset: int, parent: NodeNG)[source]#

Bases: MultiLineWithElseBlockNode, Statement

Class representing a ast.Try node.

>>> import astroid
>>> node = astroid.extract_node('''
    try:
        do_something()
    except Exception as error:
        print("Error!")
    finally:
        print("Cleanup!")
    ''')
>>> node
<Try l.2 at 0x7f23b2e41d68>
block_range(lineno: int) tuple[int, int][source]#

Get a range from a given line number to where this node ends.

body: list[NodeNG]#

The contents of the block to catch exceptions from.

finalbody: list[NodeNG]#

The contents of the finally block.

get_children()[source]#

Get the child nodes below this node.

handlers: list[ExceptHandler]#

The exception handlers.

orelse: list[NodeNG]#

The contents of the else block.

postinit(*, body: list[NodeNG], handlers: list[ExceptHandler], orelse: list[NodeNG], finalbody: list[NodeNG]) None[source]#

Do some setup after initialisation.

Parameters:
  • body – The contents of the block to catch exceptions from.

  • handlers – The exception handlers.

  • orelse – The contents of the else block.

  • finalbody – The contents of the finally block.