Java Reference
In-Depth Information
You learned how to actually create and throw your own methods by defining new excep-
tion classes and by throwing instances of any exception classes using throw .
Assertions enable you to use conditional statements and Booleans to indicate that a pro-
gram is running correctly. When this isn't the case, an assertion exception is thrown.
Threads enable you to run the most processor-intensive parts of a Java class separately
from the rest of the class. This is especially useful when the class is doing something
computing-intensive such as animation, complex mathematics, or looping through a large
amount of data quickly.
You also can use threads to do several things at once and to start and stop threads exter-
nally.
Threads implement the Runnable interface, which contains one method: run() . When
you start a thread by calling its start() method, the thread's run() method is called
automatically.
Q&A
Q I'm still not sure I understand the differences between exceptions, errors, and
runtime exceptions. Is there another way of looking at them?
A Errors are caused by dynamic linking or virtual machine problems and are thus too
low-level for most programs to care about—or be able to handle even if they did
care about them.
Runtime exceptions are generated by the normal execution of Java code, and
although they occasionally reflect a condition you will want to handle explicitly,
more often they reflect a coding mistake made by the programmer and thus simply
need to print an error to help flag that mistake.
Exceptions that are nonruntime exceptions ( IOException exceptions, for example)
are conditions that, because of their nature, should be explicitly handled by any
robust and well-thought-out code. The Java class library has been written using
only a few of these, but those few are important to using the system safely and cor-
rectly. The compiler helps you handle these exceptions properly via its throws
clause checks and restrictions.
Q How do assertions compare to unit testing for making Java programs more
reliable?
A Unit testing, like assertions, are a technique for assuring the reliability of software
by adding tests. JUnit, an open source library available from the website http://
www.junit.org, is the most popular unit-testing framework for Java programmers.
7
 
Search WWH ::




Custom Search