Back

Debugging

Reading

Errors

There are 2 types of errors that can occur when programming: compile time errors and run time errors.

Compile time errors

Compile time errors occur before your program even runs and usually have something to do with incorrect syntax: unbalanced brackets, mistyped variable name, missing semicolon, etc...

Run time errros

Run time errors occur while your sketch is running and can be broken into 2 parts: unrecoverable (program crashes) and recoverable (program continues to run but output is wrong).

The first error is most often an array index out of bounds or reference error, which basically means you're trying to access an item that simply isn't there.

The second is harder to debug and it simply means the sketch does not perform the way it should, this includes weird glitches, oddities and things simply not showing up.

Balancing Brackets

Two strategies will help with balancing brackets.

Always auto format your code using ctrl - t, this makes it easy to see that brackets may be missing.

Processing also has a bracket highlighter which highlights the starting or ending bracket, this makes it easier to track where brackets may be missing.

Bracket Example

Print

print and println are your best friends for solving run time errors. Whether it's printing out a looping variable like i or j, or the position of an object, you should always use print and println to debug run time errors.

Tracing

Tracing

You should be able to follow simple examples such as these and trace the path of execution. Obviously real examples are more complex but the same rules apply.

The Stack

In a running program: function calls that have been made at any time are placed onto "the stack".

When a program crashes and shows all the function calls that led to the crash, this is referred to as the "stack trace".

You should be comfortable with tracing your own code when your program is not running in order to determine errors. In other words, build a stack trace in your brain!