Error Handling and Debugging

Error Handling and Debugging

leJOS NXJ provides several features for error handling and debugging, including:

  • Exceptions
  • Data Aborts
  • Debugging

The Remote Monitoring and Tracing facility, which is described in its own section below, can also be used for debugging.


Most of the standard Java language exception classes are supported by leJOS, and user can create their own exception classes.


The following simplified version of the ExceptionTest example demonstrates what happens for an exception that is not caught – in this case an ArrayIndexOutOfBounds exception.

import lejos.nxt.*;

public class ExceptionTest {
  public static void main(String[] args) {
    SensorPort p = SensorPort.PORTS[5];

Data Aborts

If the leJOS firmware crashes you will normally a Data Abort. The screen shows the PC value where the failure occurred, and other details of the failure.

The screen is something like:


PC 00140BAC

AASR 1831BF01

ASR 00020601


DEBUG1 00020010

DEBUG2 00000000

The most common reason for data aborts is executing a file that is not a leJOS NXJ binary, or executing an incomplete leJOS NXJ file.

If you get a data abort in any other case, you should report the error to the leJOS development team by posting the details on the leJOS NXJ forums.

Back to top

Remote Debugging

You can use your PC as a remote console to display tracing statements generated your NXJ program. The lejos.nxt.comm.RConsole class has methods to it. Since there are no instances of this class, all methods are static.

To start debugging, you use one of these methods:

  • void open()

    opens a USB connection with no timeout

  • void openUSB(int timeout)

  • void openBluetooth(int timeout)

The NXT displays USB Console.. or BT Console.

and waits for the PC based monitor to connect.

Then execute the nxjconsole program on your PC. When the connection is established, the NXT displays Got Connection and, after some seconds, both the NXT and the PC display Console open.

If you use the variant of open with a timeout, it waits the specified number of seconds and if the debug monitor has not connected, proceeds without debugging. If the timeout is zero, it waits indefinitely.

You can also use the ConsoleViewer application to display the output.

Debug statements can be output using one of the methods:

  • void println(String s)

  • void print(String s);

If no successful open statement has been executed, the debug output is discarded. If there was a successful output, the string appears on standard out in the window or terminal that nxjconsole was run from, on the PC.

When debugging is completed, you should call:

  • void close()

This closes the USB or Bluetooth connection.


import lejos.nxt.*;
import lejos.nxt.comm.*;

/** * example using RConsole*/
public class TestRConsole {  
  public static void main(String[] args) {;
    RConsole.println("Start for loop ");
    for (int i = 0; i < 5; i++) {
      RConsole.print(" " + i);
      LCD.drawInt(i, 2, 2 * i, 4);
    RConsole.println("\n done ");

Back to top