SE251:StudentClarifications:Lec7

From Marks Wiki
Jump to navigation Jump to search

Lecture 7 - Exception Handling

Lecture slides

Misinformation

Dividing by 0 is not a compile error! Even if you blatantly divide by 0 like:

int a = b / 0;

Then the compiler will not complain (I tried it, not even a warning). It's when you run the program that it actually crashes. Java includes a class RuntimeException, but that is an exception thrown by the java runtime environment, it's not named RuntimeException to distinguish from compile time exceptions. There are no compile time exceptions, just compile errors.

Catching an exception

public static int main()
{
	try {
		int a = 1;
		int b = a - 1;
		int c = a / b;
		System.out.println(c);
	} catch (Exception e) {
		Systen.out.println("Error! Divide by 0!");
	}
}

When java gets to the line c = a / b it will find that b is 0. It can't divide by 0, so it will "throw" an exception. Specifically, ArithmeticException. Java will then ignore the rest of the code in the try block and jump straight to the catch block. If there is no catch block (or no try block) it will jump right out of the program and java will print out a message. So this program will print "Error! Divide by 0!". It will NOT print out c, because that line was skipped.

Why would you do this? Say you wanted to write something at the end of a file. You try to open the file, but it doesn't exist. You don't want to write stuff to the file any more, because it doesn't exist. You just want to print a message like "Append failed". If you throw an exception when you found it doesn't exist, the code for writing the file will be skipped automatically, and you can print the message inside the catch. Or if you found the file, but when you tried to write stuff to it you ran out of disk space, you can just throw an exception again. It will skip writing the rest of the stuff, and you can use the same catch to print the message again.

Exceptions are just classes. In the example above, I caught an exception of type Exception. All exceptions, e.g. NullPointerException, inherit from Exception, so my catch will catch any exception. You could catch NullPointerException exclusively, by writing catch (NullPointerException e). Then if any other exception is thrown, it will have to look further for a catch block to handle it. You can write several catch blocks to go with a single try. Java will look through each of the catch blocks and use the first one it finds that handles the type of exception.

Throwing an exception

Sometimes you'll have your own type of error, for example, a user might enter something that doesn't make sense. To handle this with an exception, you just have to write a class that extends Exception (or extends something that extends from Exception). Then write:

if (some_unusual_condition)
	throw new MyException();

It will jump straight to an appropriate catch block, just like a built in exception. Because exceptions are classes, you can store data in an exception object which you can then access when you catch it. This is usually details about the problem or an error message, but it could be anything.