Errors

There are three types of errors in programming (from simplest to most complex):

Syntax errors

These are grammatical errors. They are also called "compile-time errors". Java will normally not compile your program when these exist. They are the easiest to fix

Runtime errors

These are when the computer cannot do what you want or intend.

Example:

Runtime errors are examined in more detail below.

Logic errors

These occur when the program does not do what you wanted it to do. It is because of mistakes that you make in programming.

Example: when you play tictactoe it puts an X in all of the squares because you made a mistake in changing the player turn.

Example: if (isPlaying = true) { ... }
This assignes TRUE to the boolean! That's why we don't use = or == for boolean IF statements. It's too easy to screw up.
Instead write if (isPlaying) { ... } or if (! isPlaying) { ... }

Logic errors are best tracked down by

BETTER than System.out.println(): write your own DEBUGGER CLASS.


package mh_util;

/* Usage:
 * 
 * 1. import this class (it has to be in the same Java project)
 * 2. at the beginning of your program write: Debugger.setEnable(true);
 * 3. to print out variables, instead of System.out.print, use
 * 	  Debugger.log("x=" + x);
 * 4. to disable the debugger, just Debugger.setEnable(false);
 * 
 */
public class Debugger {
	
	private static boolean isEnabled = false;
	
	public static void setEnable(boolean b) {
		isEnabled = b;
	}
	
	public static void log(Object o) {
		if (Debugger.isEnabled) {
			System.out.println("DEBUG: " + o.toString());			
			//or write to a file
		}
	}
}

Java Errors and Exceptions

Java has three types of errors:

The Error class is for things that are really bad and you can't do anything about them. For example if your computer runs out of RAM or something.
All of the other errors are called EXCEPTIONS

Continue on to try-catch to finish learning about error handling.

http://javapapers.com/core-java/try-with-resources/