Ví dụ, chúng tôi có thể tham khảo các API cơ sở dữ liệu Java khi chúng ta đang nói về các thiết lập của các lớp học giúp chúng ta viết chương trình tương tác với một cơ sở dữ liệu. Một ví dụ khác của một API là Swing Java API, | exceptions 457 We can define our own exceptions by deriving a new class from Exception or one of its descendants. The class we choose as the parent depends on what situation or condition the new exception represents. The program in Listing instantiates an exception object and throws it. The exception is created from the OutOfRangeException class which is shown in Listing . Note that this exception is not part of the Java standard class library. It was created to represent the situation in which a value is outside a particular valid range. A new exception is defined by deriving a new class from the Exception class or one of its descendants. key After reading in an input value the main method evaluates it to see whether it is in the valid range. If not the throw statement is executed. A throw statement is used to begin exception propagation. Because the main method does not catch and handle the exception the program will terminate if the exception is thrown printing the message associated with the exception. We create the OutOfRangeException class by extending the Exception class. Often a new exception is nothing more than what you see in this example an extension of some existing exception class that stores a particular message describing the situation it represents. The important point is that the class is ultimately a descendant of the Exception class and the Throwable class which gives it the ability to be thrown using a throw statement. 458 CHAPTER 8 exceptions and i o streams figure Part of the Error and Exception class hierarchy The type of situation handled by this program in which a value is out of range does not need to be represented as an exception. We ve previously handled such situations using conditionals or loops. Whether you handle a situation using an exception or whether you take care of it in the normal flow of your program is an important design decision. exceptions 459 listing Author Lewis Loftus .