0

I have a notebook that I'm using to learn, and I'd like to "keep" the errors in there as instructions to myself on what not to do. Here's a basic example:

# (1b) Rename with a list of data
df['cost_bad'] = [1,2] # <-- this will error, since the length of the List/Series must match the numRows
df['cost_good'] = ...

However, this will (obviously) error on the first python statement and never run to the second one. Is there a way to set the notebook to execute all statements, even if a previous on errors? I know I can do something like:

try:
    df['cost_bad'] = [1,2] # <-- this will error, since the length of the List/Series must match the numRows
except:
    traceback.print_exc()

But I was wondering if I could do this without the try/except and just continue execution.

Trenton McKinney
  • 29,033
  • 18
  • 54
  • 66
samuelbrody1249
  • 2,819
  • 4
  • 18
  • Put the exact exception (e.g. `excet ValueError as e: print(e)`) – Trenton McKinney Aug 15 '20 at 20:20
  • If you want the stack trace, you can use `traceback.print_exc()` in your exception handler: https://docs.python.org/3/library/traceback.html#traceback.print_exc – Samwise Aug 15 '20 at 20:21
  • @TrentonMcKinney no I'm saying I don't want to catch it at all. Just for the python statements to run one after another...but assuming that isn't possible? – samuelbrody1249 Aug 15 '20 at 20:21
  • @Samwise it's about the jupyter execution not really the python try/except that I'm asking about. – samuelbrody1249 Aug 15 '20 at 20:23
  • 1
    Maybe [How to automatically execute next cell even if an error occurs in the current cell in Jupyter?](https://stackoverflow.com/questions/56402480/how-to-automatically-execute-next-cell-even-if-an-error-occurs-in-the-current-ce), but Jupyter won't execute a cell if there is a error, which is why you should use `try except continue.` – Trenton McKinney Aug 15 '20 at 20:25
  • 1
    [Jupyter: trick to run next cell even if previous cell fails](https://stackoverflow.com/questions/57364510/jupyter-trick-to-run-next-cell-even-if-previous-cell-fails), but it won't continue running things in that cell. – Trenton McKinney Aug 15 '20 at 20:26
  • 1
    @TrentonMcKinney oh got it, yes that's what I was curious about -- thanks for those links. – samuelbrody1249 Aug 15 '20 at 20:27

0 Answers0