19.09.2015 Views

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

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

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

and <strong>to</strong> modify. Be aware, however, that exception handling usually requires more time and<br />

resources, because it requires instantiating a new exception object, rolling back the call stack,<br />

and propagating the exception through the chain of methods invoked <strong>to</strong> search for the handler.<br />

An exception occurs in a method. If you want the exception <strong>to</strong> be processed by its caller,<br />

you should create an exception object and throw it. If you can handle the exception in the<br />

method where it occurs, there is no need <strong>to</strong> throw or use exceptions.<br />

In general, <strong>com</strong>mon exceptions that may occur in multiple classes in a project are candidates<br />

for exception classes. Simple errors that may occur in individual methods are best handled<br />

without throwing exceptions. This can be done by using if statements <strong>to</strong> check for<br />

errors.<br />

When should you use a try-catch block in the code? Use it when you have <strong>to</strong> deal with<br />

unexpected error conditions. Do not use a try-catch block <strong>to</strong> deal with simple, expected situations.<br />

For example, the following code<br />

try {<br />

System.out.println(refVar.<strong>to</strong>String());<br />

}<br />

catch (NullPointerException ex) {<br />

System.out.println("refVar is null");<br />

}<br />

is better replaced by<br />

if (refVar != null)<br />

System.out.println(refVar.<strong>to</strong>String());<br />

else<br />

System.out.println("refVar is null");<br />

Which situations are exceptional and which are expected is sometimes difficult <strong>to</strong> decide. The<br />

point is not <strong>to</strong> abuse exception handling as a way <strong>to</strong> deal with a simple logic test.<br />

✓Point✓ Check<br />

14.22 The following method checks whether a string is a numeric string:<br />

public static boolean isNumeric(String <strong>to</strong>ken) {<br />

try {<br />

Double.parseDouble(<strong>to</strong>ken);<br />

return true;<br />

}<br />

catch (java.lang.NumberFormatException ex) {<br />

return false;<br />

}<br />

}<br />

Is it correct? Rewrite it without using exceptions.<br />

Key<br />

Point<br />

14.7 Rethrowing Exceptions<br />

<strong>Java</strong> allows an exception handler <strong>to</strong> rethrow the exception if the handler cannot<br />

process the exception or simply wants <strong>to</strong> let its caller be notified of the exception.<br />

The syntax for rethrowing an exception may look like this:<br />

try {<br />

statements;<br />

}<br />

catch (TheException ex) {

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

Saved successfully!

Ooh no, something went wrong!