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.
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
Identify when to extend object, thread Java's classes will be used as your base classes. That is, at some point in a class hierarchy, your own classes will extend one of Java's predefined classes, available from one of Java's class libraries. Four classes that you are likely to extend most often.
- Extend Object: When you only want the basic behavior for a class, that is, the ability to create new objects using
new
, and the ability to find out some basic attributes of an object, such as which class it belongs to.
If you want to extend class Object, you do not need to do so explicitly, because Java extends class Object.
In fact, the preferred way to extend class Object for a new class called MyClass is to simply write:
class MyClass {
// your class definition...
}
- Extend Thread to implement another thread of execution. You will learn in the advanced class how to provide the behavior for your new thread so that it performs its own special behavior.
Wli>AbstractList: This class provides a skeletal implementation of the `List` interface, making it easier to create custom list types. You primarily need to implement the methods for adding and retrieving elements.
- HttpServlet:
The foundation for building Java servlets. By extending this class and overriding its methods (like `doGet` and `doPost`), you can handle HTTP requests and generate dynamic web content.
Legacy Classes which were extended prior to Java 2
- Extend Applet when you create your own applet to run within a Web browser.
Extend Panel to organize your user interface. Panels help group and display other user interface components,
such as buttons, check boxes, and so on. Panels will be discussed in the class on graphical user interfaces.
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:
- The name of the exception, such as StringIndexOutOfBoundsException
- 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.