19.09.2015 Views

Prentice.Hall.Introduction.to.Java.Programming,.Brief.Version.9th.(2014).[sharethefiles.com]

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

532 Chapter 14 Exception Handling and Text I/O<br />

A test program that uses the new Circle class is given in Listing 14.8.<br />

try<br />

catch<br />

LISTING 14.8<br />

TestCircleWithException.java<br />

1 public class TestCircleWithException {<br />

2 public static void main(String[] args) {<br />

3 try {<br />

4 CircleWithException c1 = new CircleWithException(5);<br />

5 CircleWithException c2 = new CircleWithException(-5);<br />

6 CircleWithException c3 = new CircleWithException(0);<br />

7 }<br />

8 catch (IllegalArgumentException ex) {<br />

9 System.out.println(ex);<br />

10 }<br />

11<br />

12 System.out.println("Number of objects created: " +<br />

13 CircleWithException.getNumberOfObjects());<br />

14 }<br />

15 }<br />

java.lang.IllegalArgumentException: Radius cannot be negative<br />

Number of objects created: 1<br />

The original Circle class remains intact except that the class name is changed <strong>to</strong><br />

CircleWithException, a new construc<strong>to</strong>r CircleWithException(newRadius) is<br />

added, and the setRadius method now declares an exception and throws it if the radius is<br />

negative.<br />

The setRadius method declares <strong>to</strong> throw IllegalArgumentException in the method<br />

header (lines 25–32 in CircleWithException.java). The CircleWithException class would<br />

still <strong>com</strong>pile if the throws IllegalArgumentException clause were removed from the<br />

method declaration, since it is a subclass of RuntimeException and every method can<br />

throw RuntimeException (an unchecked exception) regardless of whether it is declared in<br />

the method header.<br />

The test program creates three CircleWithException objects—c1, c2, and c3—<strong>to</strong> test<br />

how <strong>to</strong> handle exceptions. Invoking new CircleWithException(-5) (line 5 in Listing 14.8)<br />

causes the setRadius method <strong>to</strong> be invoked, which throws an IllegalArgumentException,<br />

because the radius is negative. In the catch block, the type of the object ex is<br />

IllegalArgumentException, which matches the exception object thrown by the setRadius<br />

method, so this exception is caught by the catch block.<br />

The exception handler prints a short message, ex.<strong>to</strong>String() (line 9 in Listing 14.8),<br />

about the exception, using System.out.println(ex).<br />

Note that the execution continues in the event of the exception. If the handlers had not<br />

caught the exception, the program would have abruptly terminated.<br />

The test program would still <strong>com</strong>pile if the try statement were not used, because the method<br />

throws an instance of IllegalArgumentException, a subclass of RuntimeException (an<br />

unchecked exception). If a method throws an exception other than RuntimeException or<br />

Error, the method must be invoked within a try-catch block.<br />

✓Point✓ Check<br />

14.9 What is the purpose of declaring exceptions? How do you declare an exception, and<br />

where? Can you declare multiple exceptions in a method header?<br />

14.10 What is a checked exception, and what is an unchecked exception?<br />

14.11 How do you throw an exception? Can you throw multiple exceptions in one throw<br />

statement?<br />

14.12 What is the keyword throw used for? What is the keyword throws used for?

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!