This site is retired and is no longer updated since November 2021. Please visit our new website at https://www.teamscode.org for up-to-date information!
Errors/Exceptions

Code Bar
throw Throw Keyword
try {} catch() {} Try-Catch Structure
ArithmeticException The exception thrown when mathematical rules are broken.
IndexOutOfBoundsException The exception thrown when someone tries to access an index out of bound.
NullPointerException The exception thrown when someone tries to call a undefined variable.
Exception The super-class to all exceptions.

Many things can go wrong while your code is running. So the developers added exceptions to handle the such problems. Above is a list of the most often occurring exceptions, but you only need to remember the basic Exception.

As you can clearly see, the first two terms in the code bar are not exceptions. The first vocab word is throw. Throws is a keyword that can be inserted after the parameters of a method to basically allow the computer to run the risky code. For example the main method below would not compile, if you didn’t add the FileNotFoundException.

	public static void main(String[] args) throws FileNotFoundException {

		Scanner input = new Scanner(new File(""));

	}

The throw keyword can also be used to end a program and print an exception. For example, look at the code below.

	public static int divide(int a, int b) {

		Return a / b;

	}

If you inputed 0 as b, your code would break down, because anything divided by 0 is undefined. In fact, an ArithmeticException would be thrown. So to improve the code, you should check whether b is 0, and if it is throw an exception. Instead of the computer throwing the exception, we are, and by throwing the exception we can gain more debugging information.

	public static int divide(int a, int b) {

		if (b == 0) {

			throw new IllegalArgumentException();

		}

		return a / b;

	}

Now anytime b is entered in as 0, the program catches it and prints out the IllegalArgumentException. Throw, however, can be used to throw any exception; even ones not pertaining to the code. The code below would’ve worked just as well.

	public static int divide(int a, int b) {

		if (b == 0) {

			throw new FileNotFoundException();

		} else {

			return a / b;

		}

	}

The second item in the code bar is the try-catch structure. The try-catch structure becomes more important the further you delve into computer science. Like the throw keyword, it allows you to run risky code; however, the try-catch structure has two more bodies.

	try {

		// code

	}

This is the first and most important part of the try-catch structure. You would put your risky code inside those brackets, and the computer will “try” to run it. It will proceed normally until, if at all, it reaches an error. Once it does reach an error, Java will stop the code and jump the catch block.

	catch (Exception e) {

		e.printStackTrace();

	}

The next part is the catch block. The catch block is executed if and only if the exception specified in the parenthesis was thrown in the try block; by only delegating to catch blocks the specified exception, the developers allowed multiple catch blocks in a try-catch structure. Importantly every try-catch structure needs at least one. As you may have noticed, I put e.printStackTrace() in the catch block; This method is called on when the exception is thrown, and it prints debugging data.

	catch (FileNotFoundException e) {

		e.printStackTrace();

	}
	
	catch (ArithmeticException e) {

		e.printStackTrace();

	}
	
	catch (IndexOutOfBoundException e) {

		e.printStackTrace();

	}

The remaining topics you need to learn are the exceptions. There are many exceptions in Java and most of them are rarely used. I have added a list of the most useful and common exceptions; and I will explain what the exceptions mean. But first you need to know the division of exceptions: checked and unchecked exceptions.

Checked exceptions are usually thrown when your code is compiled. In general checked exceptions are not found errors. For example if you misspell a word and cause an error, that is likely a checked exception.

The important checked exceptions are: ClassNotFoundException, NoSuchFieldException, and NoSuchMethodException - these are called when you try to access a nonexistent thing. They are not included in my list, because any good IDE will highlight them for you.

Unchecked exceptions are exceptions thrown will the code is running. They constitute a much wider range of exceptions and they occur much more often. Good examples are the ArithmeticException, IndexOutofBoundsException, and NullPointerException.

Exception:

The basic exception is the superclass to all exceptions. Therefore when you don’t know what exception should be thrown, you can simply just throw the basic exception. Commonly it will be added as the last catch block, as a just in case.

ArithemeticException:

This exception is only thrown when you try to break mathematical rules. For example, dividing by 0 or taking the square root of a negative number will throw this exception.

IndexOutOfBoundsException:

This exception is thrown when you try to retrieve or set a value outside of the bounds of the object you’re calling from. For example, if you try to get the object at index -1 or if you try to get the object after the last object in an list. There are two additional exceptions which function similarly: StringIndexOutOfBoundsException and ArrayIndexOutOfBoundsException. The only difference is the object from which the data is being accessed.

NullPointerException:

This exception is one of the most common. It is thrown when you try to access the value of a null object (such as an uninitialized variable).

Written by Jason Zhang

Notice any mistakes? Please email us at learn@teamscode.com so that we can fix any inaccuracies.