Handling Errors using Exceptions |
The pages listed below explain how to throw exceptions in a Java program.The throw Statement
Before you can catch an exception, some Java code somewhere must throw one. Any Java code can throw an exception: your code, code from a package written by someone else (such as the packages that come with the Java development environment), or the Java runtime system. Regardless of who (or what) throws the exception, it's always thrown with the Javathrow
statement.The Throwable Class and Its Subclasses
The Java language requires that all objects you throw with thethrow
statement derive from the Throwable class or one of its subclasses.Creating Your Own Exception Classes
As you have undoubtedly noticed in your travels (travails?) through the Java language, the packages that ship with the Java development environment provide numerous error and exception classes. All of these classes are descendents of the Throwable class and allow programs to differentiate between the various types of errors or exceptions that can occur during the execution of a Java program.You, as a Java programmer, can create your own error and exception classes as well to create a set of classes that represents problems that can occur within the classes that you write. Indeed, if you are a package developer you will find that you must create their own set of exceptions classes to allow your users to differentiate an error that can occur in your package versus those errors that occur in the Java development environment or other packages.
Handling Errors using Exceptions |