This page was generated from doc/allow-errors-per-cell.ipynb. Interactive online version: Binder badge.

Ignoring Errors on a Per-Cell Basis

Instead of ignoring errors for all notebooks or for some selected notebooks (see the previous notebook), you can be more fine-grained and just allow errors on certain code cells by tagging them with the raises-exception tag.

[1]:
'no problem'
[1]:
'no problem'

The following code cell has the raises-exception tag.

[2]:
problem
Unexpected exception formatting exception. Falling back to standard exception
Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3398, in run_code
    exec(code_obj, self.user_global_ns, self.user_ns)
  File "/tmp/ipykernel_709672/2867838948.py", line 1, in <cell line: 1>
    problem
NameError: name 'problem' is not defined

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 317, in executing
    args = executing_cache[key]
KeyError: (<code object run_code at 0x7f7f378c4f50, file "/usr/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 3362>, 140184369516368, 74)

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/usr/lib/python3.8/site-packages/IPython/core/interactiveshell.py", line 1993, in showtraceback
    stb = self.InteractiveTB.structured_traceback(
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 1118, in structured_traceback
    return FormattedTB.structured_traceback(
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 1012, in structured_traceback
    return VerboseTB.structured_traceback(
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 865, in structured_traceback
    formatted_exception = self.format_exception_as_a_whole(etype, evalue, etb, number_of_lines_of_context,
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 799, in format_exception_as_a_whole
    self.get_records(etb, number_of_lines_of_context, tb_offset) if etb else []
  File "/usr/lib/python3.8/site-packages/IPython/core/ultratb.py", line 854, in get_records
    return list(stack_data.FrameInfo.stack_data(etb, options=options))[tb_offset:]
  File "/usr/lib/python3.8/site-packages/stack_data/core.py", line 565, in stack_data
    yield from collapse_repeated(
  File "/usr/lib/python3.8/site-packages/stack_data/utils.py", line 84, in collapse_repeated
    yield from map(mapper, original_group)
  File "/usr/lib/python3.8/site-packages/stack_data/core.py", line 555, in mapper
    return cls(f, options)
  File "/usr/lib/python3.8/site-packages/stack_data/core.py", line 520, in __init__
    self.executing = Source.executing(frame_or_tb)
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 372, in executing
    args = find(source=cls.for_frame(frame), retry_cache=True)
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 252, in for_frame
    return cls.for_filename(frame.f_code.co_filename, frame.f_globals or {}, use_cache)
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 270, in for_filename
    result = source_cache[filename] = cls._for_filename_and_lines(filename, lines)
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 281, in _for_filename_and_lines
    result = source_cache[(filename, lines)] = cls(filename, lines)
  File "/usr/lib/python3.8/site-packages/stack_data/core.py", line 81, in __init__
    self.asttokens()
  File "/usr/lib/python3.8/site-packages/executing/executing.py", line 416, in asttokens
    return ASTTokens(
  File "/usr/lib/python3.8/site-packages/asttokens/asttokens.py", line 62, in __init__
    self.mark_tokens(self._tree)
  File "/usr/lib/python3.8/site-packages/asttokens/asttokens.py", line 73, in mark_tokens
    MarkTokens(self).visit_tree(root_node)
  File "/usr/lib/python3.8/site-packages/asttokens/mark_tokens.py", line 47, in visit_tree
    util.visit_tree(node, self._visit_before_children, self._visit_after_children)
  File "/usr/lib/python3.8/site-packages/asttokens/util.py", line 184, in visit_tree
    ret = postvisit(current, par_value, value)
  File "/usr/lib/python3.8/site-packages/asttokens/mark_tokens.py", line 90, in _visit_after_children
    nfirst, nlast = self._methods.get(self, node.__class__)(node, first, last)
  File "/usr/lib/python3.8/site-packages/asttokens/mark_tokens.py", line 175, in visit_listcomp
    return self.handle_comp('[', node, first_token, last_token)
  File "/usr/lib/python3.8/site-packages/asttokens/mark_tokens.py", line 171, in handle_comp
    util.expect_token(before, token.OP, open_brace)
  File "/usr/lib/python3.8/site-packages/asttokens/util.py", line 56, in expect_token
    raise ValueError("Expected token %s, got %s on line %s col %s" % (
ValueError: Expected token OP:'[', got OP:'+' on line 1325 col 73

The following code cell is executed even though the previous cell raised an exception.

[3]:
'no problem'
[3]:
'no problem'

Note

The behavior of the raises-exception tag doesn’t match its name. While it does allow exceptions, it does not check if an exception is actually raised!

This will hopefully be fixed at some point, see https://github.com/jupyter/nbconvert/issues/730.