Skip to Main Content

Understanding error messages

Programmers read for a living

  • We read docs. 
  • We read code.
  • We especially read error messages.

Parts of an error

Examples from several languages

Here are three programming language error messages.  Below each message is a list of features to look for in the error message that can be informative for debugging.  In particular, notice that for Python and R the last item in the list has the error, but for Java the first line of the error is often most informative to tell you what line of code has the problem.

Python error

Traceback (most recent call last):

  File "/home/lmc/Projects/debug-workshop/example1.py", line 17, in <module>

    exampleFunction(test)

  File "/home/lmc/Projects/debug-workshop/example1.py", line 11, in exampleFunction

    calc = foo(far(vals))[i]

IndexError: list index out of range

  • Line number(s) (here, 11 and 17)
  • Functions (or other structures) 
  • Message about what went wrong ("list index out of range") in final line
R error

Error in x[, 11] : subscript out of bounds

  • No line number(s), but 11 indicates a column number for this matrix object.
  • Functions (or other structures, here a matrix object)
  • Message about what went wrong "subscript out of bounds" in final line
Java error

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for length 4

        at Example2.BuggyProgram.baz(BuggyProgram.java:13)

        at Example2.BuggyProgram.main(BuggyProgram.java:24)

  • Line number(s) (lines 13 and 24 have errors)
  • Functions (or other structures) 
  • Message about what went wrong (index 4 out of bounds for length 4), but in the first line, not the final line.

Summary of common formats

  • Most important lines are usually first or last.
  • Scan for keywords like Error or Exception.
  • Then read carefully to get more context about line numbers or data structure numbers (columns, rows)

Can't figure out the message?

  • You’re unlikely to be the first person to see a particular error.
  • Ask a search engine!
  • You may need to remove pieces of the error like local variable names that are specific to your project.

 

Quiz