Note
Click here to download the full example code or to run this example in your browser via Binder
Example that fails to execute¶
This example demonstrates a code block that raises an error and how any code blocks that follow are not executed.
When scripts fail, their gallery thumbnail is replaced with the broken image stamp. This allows easy identification in the gallery display.
You will also get the python traceback of the failed code block.
15 16 17 18 19 20 21 22 | # Code source: Óscar Nájera
# License: BSD 3 clause
# sphinx_gallery_line_numbers = True
import numpy as np
import matplotlib.pyplot as plt
plt.pcolormesh(np.random.randn(100, 100))
|

Out:
<matplotlib.collections.QuadMesh object at 0x7f3ff8d18d90>
This next block will raise a NameError
27 | iae
|
Traceback (most recent call last):
File "/build/sphinx-gallery-cyOzbg/sphinx-gallery-0.7.0/.pybuild/cpython3_3.8_sphinx-gallery/build/sphinx_gallery/gen_gallery.py", line 146, in call_memory
mem, out = memory_usage(func, max_usage=True, retval=True,
File "/usr/lib/python3/dist-packages/memory_profiler.py", line 333, in memory_usage
returned = f(*args, **kw)
File "/build/sphinx-gallery-cyOzbg/sphinx-gallery-0.7.0/.pybuild/cpython3_3.8_sphinx-gallery/build/sphinx_gallery/gen_rst.py", line 466, in __call__
exec(self.code, self.fake_main.__dict__)
File "/build/sphinx-gallery-cyOzbg/sphinx-gallery-0.7.0/examples/no_output/plot_raise.py", line 27, in <module>
iae
NameError: name 'iae' is not defined
Sphinx gallery will stop executing the remaining code blocks after the exception has occurred in the example script. Nevertheless the html will still render all the example annotated text and code blocks, but no output will be shown.
Here is another error raising block but will not be executed
38 | plt.plot('Strings are not a valid argument for the plot function')
|
Estimated memory usage: 188 MB