Throwing Exceptions

When you call a method of another class, that class can control how the method is used by throwing exceptions.

As you make use of the classes in the Java class library, the compiler often displays a message such as the following:

Output


NetReader.java:14: unreported exception java.net.MalformedURLException; must be caught or declared to be thrown


Whenever you see an error stating that an exception “must be caught or declared to be thrown,” it indicates the method you are trying to use throws an exception.

Any class that calls these methods, such as an application that you write, must do one of the following things:

• Handle the exception with a try-catch block.

• Throw the exception.

• Handle the exception with a try-catch block and then throw it.

Up to this point in the hour, you have seen how to handle exceptions. If you would like to throw an exception after handling it, you can use a throw statement followed by the exception object to throw.

The following statements handle a NumberFormatException error in a catch block, and then throw the exception:

try {
    principal = Float.parseFloat(loanText) * 1.1F;
} catch (NumberFormatException e) {
    System.out.println(arguments[i] + " is not a number.");
    throw e;
}

This rewritten code handles all exceptions that could be generated in the try block and throws them:

try {
    principal = Float.parseFloat(loanText) * 1.1F;
} catch (Exception e) {
    System.out.println("Error " + e.getMessage());
    throw e;
}

Exception is the parent of all exception subclasses. A catch statement will catch the class and any subclass below it in the class hierarchy.

When you throw an exception with throw, it generally means you have not done everything that needs to be done to take care of the exception.

An example of where this might be useful: Consider a hypothetical program called CreditCardChecker, an application that verifies credit card purchases. This application uses a class called CheckDatabase, which has the following job:

1. Make a connection to the credit card lender’s computer.

2. Ask that computer if the customer’s credit card number is valid.

3. Ask the computer if the customer has enough credit to make the purchase.

As the CheckDatabase class is doing its job, what happens if the credit card lender’s computer doesn’t answer the phone at all? This kind of error is exactly the kind of thing that the try-catch block was designed for, and it is used within CheckDatabase to handle connection errors.

If the CheckDatabase class handles this error by itself, the CreditCardChecker application doesn’t know that the exception took place at all. This isn’t a good idea—the application should know when a connection cannot be made so it can report this to the person using the application.

One way to notify the CreditCardChecker application is for CheckDatabase to catch the exception in a catch block, and then throw it again with a throw statement. The exception is thrown in CheckDatabase, which must then deal with it like any other exception.

Exception handling is a way that classes can communicate with each other in the event of an error or other unusual circumstance.

When using throw in a catch block that catches a parent class, such as Exception, throwing the exception throws that class. This loses some detail of what kind of error occurred, because a subclass such as NumberFormatException tells you a lot more about the problem than simply the Exception class.

Java 7 offers a new way to keep this detail: the final keyword in a catch statement.

try {
    principal = Float.parseFloat(loanText) * 1.1F;
} catch (final Exception e) {
    System.out.println("Error " + e.getMessage());
    throw e;
}

That final keyword in catch causes throw to behave differently. The specific class that was caught is thrown.

Ignoring Exceptions

The last technique that is covered this hour is how to ignore an exception completely. A method in a class can ignore exceptions by using a throws clause as part of the method definition.

The following method throws a MalformedURLException, an error that can occur when you are working with web addresses in a Java program:

public loadURL(String address) throws MalformedURLException {
    URL page = new URL(address);
    loadWebPage(page);
}

The second statement in this example creates a URL object, which represents an address on the Web. The constructor method of the URL class throws a MalformedURLException to indicate that an invalid address is used, so no object can be constructed. The following statement causes one of these exceptions to be thrown:

URL source = new URL("http:www.java24hours.com");

The string http:www.java24hours.com is not a valid URL. It’s missing some punctuation—two slash characters (//) after the colon.

Because the loadURL() method has been declared to throw MalformedURLException errors, it does not have to deal with them inside the method. The responsibility for catching this exception falls to any method that calls the loadURL() method.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.225.209.152