Java Basics  «Prev  Next»

Lesson 13

Java OOA Exceptions Module

The next module will discuss exceptions.
Exceptions is how Java defines a control flow for errors. You will learn how to report and respond to errors and how to make sure your methods all declare which exceptions they might throw.

Exception Definition:

An exception is an event, which occurs during the execution of a program, that disrupts the normal flow of the program's instructions
When an error occurs within a method, the method creates an object and hands it off to the runtime system. The object, called an exception object, contains information about the error, including its type and the state of the program when the error occurred. Creating an exception object and handing it to the runtime system is called throwing an exception.
After a method throws an exception, the runtime system attempts to find something to handle it.
The set of possible "unknowns" to handle the exception is the ordered list of methods that had been called to get to the method where the error occurred. The list of methods is known as the call stack
Extending Classes
Exceptions CallStack consisting of a series of method calls

You will often have programs that terminate and display an error message, such as
Exception in thread "main"
java.lang.StringIndexOutOfBoundsException:
String index out of range: -4
at java.lang.String.substring
(String.java:1444)
at Example1.main(Example1.java:16)

An amazing number of students simply give up at that point, saying "my program died", without ever reading the error message. Admittedly, the format of the exception report is not very friendly and is actually easy to decipher the exception message.
When you have a close look at the error message, you will notice two pieces of useful information:
  1. The name of the exception, such as StringIndexOutOfBoundsException
  2. The line number of the code that contained the statement that caused the exception, such as Example1.java:16
The name of the exception is always in the first line of the report, and it ends in Exception.
If you get a StringIndexOutOfBoundsException, then there was a problem with accessing an invalid position in a string and is useful information.
The line number of the offending code is a little harder to determine and the exception report contains the entire stack trace which is the names of all methods that were pending when the exception hit.
The first line of the stack trace is the method that actually generated the exception. The last line of the stack trace is a line in main. Often, the exception was thrown by a method that is in the standard library. Look for the first line in your code that appears in the exception report. For example, skip the line that refers to java.lang.String.substring(String.java:1444) The next line in our example mentions a line number in your code, Example1.java.
Once you have the line number in your code, open up the file, go to that line, and look at it! In the great majority of cases, knowing the name of the exception and the line that caused it make it completely obvious what went wrong, and you can easily fix your error.