Exceptions

Let’s start with the basics. In Java, or programming in general, what is an Exception?

An Exception is something that happens in your code that is unexpected. Sometimes it’s an error. Sometimes it’s an issue of unexpected data. Basically the computer has come across some unexpected issue and doesn’t know what to do.

Exceptions aren’t necessarily a fatal problem though. You’re able to catch an Exception, and in some cases you may be able to work around the problem. More on that later.

How about some examples?

Sure.

Let’s take a look at a few that you’ll probably come across as you code. You can also scroll to the bottom of this page to get more details on some of these.

NullPointerException

When you create an object in Java its default value is null. For example, if you type the following code

int[] ray;

ray is null. You’re setting it up to hold an int array, but right now it’s not there. And since it’s null you can’t call any of the normal array methods on it. If you tried to do System.out.println( ray.length ); you would get a NullPointerException.

DivideByZeroException

Remember when you first learned how to divide and your teacher told you that you can’t divide by zero? Yeah, Java doesn’t know how to do it either.

This code will throw a DivideByZeroException

System.out.println( 1 / 0 ); 

What’s the point of Exceptions?

This is probably the question that got you to this page. Why do we need to know if something wonky happened?

Probably most importantly you will eventually write code that will throw an Exception. You don’t mean to. But it happens to everyone. And if your code throws an Exception that’s not handled your program will crash.

What’s great about Exceptions is you don’t always have to handle it right away. You can tell your methods to just pass Exceptions up stream to let the calling method handle it.

Catching Exceptions

If you’re running something that might throw an Exception, you want to try and catch it.

Let’s say we’re using the Scanner and File classes to try and read from a data file. Think about what happens if the file we’re looking for doesn’t exist.

import java.util.Scanner;
import java.io.File;

public class Example {
    public static void main(String[] args) {
        Scanner f = new Scanner( new File( "test.dat" ) ); 
    }
}

If you tried to compile this code Java would throw an error telling you that the line starting with Scanner needs to be checked for Exceptions.

You’ve got a couple of options.

First, you could check for the Exception right there.

import java.util.Scanner;
import java.io.File;

public class Example {
    public static void main(String[] args) {
        try {
            Scanner f = new File( "test.dat" ) ); 
        }
        catch (Exception e) {
            System.out.println("Could not find the file"); 
        }
    }
}

Now, instead of the Exception killing your program your code will gracefully print Could not find the file and keep running.

But sometimes you want to just pass it upstream. You can also do something similar to this.

import java.util.Scanner;
import java.io.File;

public class Example {
    public static void main(String[] args) throws Exception {
        Scanner f = new Scanner( new File( "test.dat" ) ); 
    }
}   

This time if the new File() throws an Exception it’s thrown back up to whatever called the method. In this case it’s inside the main so your program will still crash. But you could have code in the calling method that handles the Exception.

List of Exceptions

A

ArithmeticException
Exception thrown when an impossible math condition is used. Most common example is when you try to divide by zero.

N

NullPointerException
Exception thrown when trying to access a property or method of a null object.