Blakes 21 Days Chapter 7 Document


Day 7, Exceptions and Threads

Exceptions

Exception Classes

Managing Exceptions

Exception Consistency Checking

Protecting Code and Catching Exceptions

The finally Clause

First Program

Listing 7.1

Figure 7.1 goes here

The Explanation

try-with-resources

Declaring Methods That Might Throw Exceptions

The throws Clause

Which Exceptions Should You Throw?

Passing on Exceptions

throws and Inheritance

Creating and Throwing Exceptions

Throwing Exceptions

Creating Your Own Exceptions

Combining throws, try, and throw

When Not to Use Exceptions

Bad Style Using Exceptions

Threads

Writing a Threaded Program

A Threaded Application

Listing 7.2

The Explanantion

Listing 7.3 (The Third Program)

Run Program

Figure 7.2 goes here

Explanantion

Stopping a Thread



Summary

Q & A

Quiz - Questions

  1. What keyword is used to jump out of a try block and into a finally block ?
    1. catch
    2. return
    3. while
  2. What class should be the superclass of any exceptions you create in Java ?
    1. Throwable
    2. Error
    3. Exception
  3. If a class implements the Runnable interface, what methods must the class contain ?
    1. start(), stop(), and run().
    2. actionPerformed()
    3. run()

Answers

  1. B. The return statement exits the block.
  2. C. The kinds of errors you'll want to note in your programs generally belong in the Exception hierarchy.
  3. C. The Runnable interface requires only the run() method.

Certification Practice

  1. Which statement contains an error ?
    1. for (i = 0; i < arguments.length & i< 10; i++) {
    2. sum += temps[i];
    3. System.out.println("Average: " + (sum / i));
    4. None of them, the program is correct.

Exercise



Reserve