Добавил:
Upload Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Ganesh_JavaSE7_Programming_1z0-804_study_guide.pdf
Скачиваний:
94
Добавлен:
02.02.2015
Размер:
5.88 Mб
Скачать

Chapter 11 exCeptions and assertions

In this case, you print the error in the console using a println() statement. This is a trivial program and the error occurred in the main() method, so the error handling is easy. In this case, you can terminate the program after printing the error message to the console. However, if you are deep within the function calls in a complex application, you need a better way to indicate that an “exceptional condition” has occurred and then inform the caller about that condition. Further, you often need to recover from an error condition instead of terminating the program. So you need to be able to “handle” an exception or “rethrow” that exception further up in the call stack so that a caller can handle that exception. (We’ll revisit this topic of rethrowing exceptions later in this chapter.) At present, you’ll change the program in Listing 11-1 to throw an exception instead of printing an error message (in a separate program,

Echo1.java), like so:

if(args.length == 0) {

// no arguments passed - throw an exception

throw new IllegalArgumentException("No input passed to echo command");

}

This block inside the if condition for args.length == 0 is the only part that needs to be changed within this program. Note the syntax for throwing an exception: the throw keyword followed by the exception object. Here you make use of IllegalArgumentException, which is already defined in the Java library. Later in this chapter, you’ll see how to define your own exceptions.

Now, if you run this program without passing any arguments in the command line, the program will throw an

IllegalArgumentException.

D:\> java Echo1

Exception in thread "main" java.lang.IllegalArgumentException: No input passed to echo command at Echo1.main(Echo1.java:5)

Since there was no handler for this exception, this uncaught exception terminated the program. In this case, you explicitly threw an exception. Exceptions can also get thrown when you write some code or call Java APIs. You’ll look at an example now.

Unhandled Exceptions

Consider the program in Listing 11-2, which attempts to read an integer value that the user types in the console and prints the read integer back to the console. For reading an integer from the console, you make use of the readInt() method provided in the java.util.Scanner class. To instantiate the Scanner class, you pass in System.in, which is a reference to the system input stream.

Listing 11-2. ScanInt1.java

// A simple progam to accept an integer from user

import java.util.*;

class ScanInt {

public static void main(String [] args) { System.out.println("Type an integer in the console: "); Scanner consoleScanner = new Scanner(System.in);

System.out.println("You typed the integer value: " + consoleScanner.nextInt());

}

}

319

Соседние файлы в предмете [НЕСОРТИРОВАННОЕ]