CS - 2013: 12. Java: Chapter 12 - Exception Handling and Text I/O

अब Quizwiz के साथ अपने होमवर्क और परीक्षाओं को एस करें!

Show the output of the following code. public class Test { public static void main(String[] args) { try { for (int i = 0; i < 2; i++) { System.out.print(i + " "); System.out.println(1 / 0); } } catch (Exception ex) { } } }

0

Show the output of the following code. public class Test { public static void main(String[] args) { for (int i = 0; i < 2; i++) { System.out.print(i + ", "); try { System.out.println(1 / 0); } catch (Exception ex) { } } } }

0, 1,

Which of the following statements will throw an exception? 1. System.out.println(1 / 0); 2. System.out.println(1.0 / 0);

1

In the java.lang.Throwable class, a. ......... returns the message that describes this exception object. b. ....... returns the concatenation of three strings: (1) the full name of the exception class; (2) ":" (a colon and a space); (3) the getMessage() method. c. ....... prints the Throwable object and its call stack trace information on the console. d. ......... returns an array of stack trace elements representing the stack trace pertaining to this exception object. => Use the possible following answers to fill in the above blanks: 1. getMessage(): String 2. getStackTrace(): StackTraceElement[] 3. printStackTrace(): void 4. toString(): String

1, 4, 3, 2

Suppose you run the following code: 1. public static void main(String[] args) { 2. try { 3. m(); 4. statement7; 5. } 6. catch (Exception2 ex { 7. statement8; 8. } 9 } 10. public static void m() { 11. try { 12. statement1; 13. statement2; 14. statement3; 15. } 16. catch (Exception1 ex1) { 17. statement4; 18. } 19. finally { 20. statement5; 21. } 22. statement6; 23. } If no exception occurs, which lines are executed? (in-order)

12, 13, 14, 20, 22, 4

Suppose you run the following code: 1. public static void main(String[] args) { 2. try { 3. m(); 4. statement7; 5. } 6. catch (Exception2 ex { 7. statement8; 8. } 9 } 10. public static void m() { 11. try { 12. statement1; 13. statement2; 14. statement3; 15. } 16. catch (Exception1 ex1) { 17. statement4; 18. } 19. finally { 20. statement5; 21. } 22. statement6; 23. } If statement2 throws an exception of type Exception1, which lines are executed? (in-order)

12, 13, 17, 20, 22, 4

Suppose you run the following code: 1. public static void main(String[] args) { 2. try { 3. m(); 4. statement7; 5. } 6. catch (Exception2 ex { 7. statement8; 8. } 9 } 10. public static void m() { 11. try { 12. statement1; 13. statement2; 14. statement3; 15. } 16. catch (Exception1 ex1) { 17. statement4; 18. } 19. finally { 20. statement5; 21. } 22. statement6; 23. } If statement2 throws an exception that is neither Exception1 nor Exception 2, which statements are executed? (in-order)

12, 13, 20

Suppose you run the following code: 1. public static void main(String[] args) { 2. try { 3. m(); 4. statement7; 5. } 6. catch (Exception2 ex { 7. statement8; 8. } 9 } 10. public static void m() { 11. try { 12. statement1; 13. statement2; 14. statement3; 15. } 16. catch (Exception1 ex1) { 17. statement4; 18. } 19. finally { 20. statement5; 21. } 22. statement6; 23. } If statement2 throws an exception of type Exception2, which statements are executed? (in-order)

12, 13, 20, 7

Suppose that "statement2" causes an exception in the following statement: 1. try { 2. statement1; 3. statement2; 4. statement3; 5. } 6. catch (Exception1 ex1) { } 7. finally { 8. statement4; 9. } 10. statement5; If no exception occurs, which lines will be executed? (in-order)

2, 3, 4, 8, 10

Suppose that "statement2" causes an exception in the following statement: 1. try { 2. statement1; 3. statement2; 4. statement3; 5. } 6. catch (Exception1 ex1) { } 7. finally { 8. statement4; 9. } 10. statement5; If the exception is not of type Exception1 which lines will be executed? (in-order)

2, 8

Suppose that "statement2" causes an exception in the following statement: 1. try { 2. statement1; 3. statement2; 4. statement3; 5. } 6. catch (Exception1 ex1) { } 7. finally { 8. statement4; 9. } 10. statement5; If the exception is of type Exception1, which lines will be executed? (in-order)

2, 8, 10

Suppose you enter 34, press the Enter key, then enter 567 and press the Enter key for the following code: " Scanner input = new Scanner(System.in); int intValue = input.nextInt(); String line = input.nextLine(); " => You will get ..... in "intValue" and an ...... in "line". => Why? => Here is the reason. The token-reading method nextInt() reads in 34 and stops at the ......, which in this case is a line separator (the Enter key). The nextLine() method ends after reading the line separator and returns the string read ...... the line separator. Since there are no characters before the line separator, line is ........

34, empty string, delimiter, before, empty

Suppose you enter 45 57.8 789, then press the Enter key. Scanner input = new Scanner(System.in); int intValue = input.nextInt(); double doubleValue = input.nextDouble(); String line = input.nextLine(); => intValue contains ..... doubleValue contains ....., and line contains .......

45, 57.8, ' ', '7 ', '8 ', '9'.

Suppose you enter 45, press the Enter key, 57.8, press the Enter key, 789, and press the Enter key. Scanner input = new Scanner(System.in); int intValue = input.nextInt(); double doubleValue = input.nextDouble(); String line = input.nextLine(); => intValue contains ...... doubleValue contains ....., and line is ......

45, 57.8, empty

Suppose you run the following code: 1. public static void main(String[] args) throws 2. Exception2 { 3. m(); 4. statement7; 5. } 6. public static void m() { 7. try { 8. statement1; 9. statement2; 10. statement3; 11. } 12. catch (Exception1 ex1) { 13. statement4; 14. } 15. finally { 16. statement5; 17. } 18. statement6; 19. } If no exception occurs, which lines are executed? (in-order)

8, 9, 10, 16, 18, 4

Suppose you run the following code: 1. public static void main(String[] args) throws 2. Exception2 { 3. m(); 4. statement7; 5. } 6. public static void m() { 7. try { 8. statement1; 9. statement2; 10. statement3; 11. } 12. catch (Exception1 ex1) { 13. statement4; 14. } 15. finally { 16. statement5; 17. } 18. statement6; 19. } If statement2 throws an exception of type Exception1, which lines are executed? (in-order)

8, 9, 13, 16, 18, 4

Suppose you run the following code: 1. public static void main(String[] args) throws 2. Exception2 { 3. m(); 4. statement7; 5. } 6. public static void m() { 7. try { 8. statement1; 9. statement2; 10. statement3; 11. } 12. catch (Exception1 ex1) { 13. statement4; 14. } 15. finally { 16. statement5; 17. } 18. statement6; 19. } If statement2 throws an exception of type Exception2, which lines are executed? (in-order)

8, 9, 16

Suppose you run the following code: 1. public static void main(String[] args) throws 2. Exception2 { 3. m(); 4. statement7; 5. } 6. public static void m() { 7. try { 8. statement1; 9. statement2; 10. statement3; 11. } 12. catch (Exception1 ex1) { 13. statement4; 14. } 15. finally { 16. statement5; 17. } 18. statement6; 19. } If statement2 throws an exception that is neither Exception1 nor Exception2, which lines are executed? (in-order)

8, 9, 16

What RuntimeException will the following program throw, if any? public class Test { public static void main(String[] args) { System.out.println(1 / 0); } }

ArithmeticException

What is displayed when running the following program? public class Test { public static void main(String[] args) { try { method(); System.out.println("After the method call"); } catch (ArithmeticException ex) { System.out.println("ArithmeticException"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } catch (Exception e) { System.out.println("Exception"); } } static void method() throws Exception { System.out.println(1 / 0); } }

ArithmeticException

Examples of Subclasses of RuntimeException: (possible answers: ArithmeticException, NullPointerException, IndexOutOfBoundsException, and IllegalArgumentException) 1. ........: Dividing an integer by zero. (floating-point arithmetic does not throw exceptions) 2. .......: Attempt to access an object through a null reference variable. 3.: ........ Index to an array is out of range. 4. ........: A method is passed an argument that is illegal or inappropriate.

ArithmeticException, NullPointerException, IndexOutOfBoundsException, IllegalArgumentException

What RuntimeException will the following program throw, if any? public class Test { public static void main(String[] args) { int[] list = new int[5]; System.out.println(list[5]); } }

ArrayIndexOutOfBoundsException

What RuntimeException will the following program throw, if any? public class Test { public static void main(String[] args) { Object o = new Object(); String d = (String)o; } }

ClassCastException

Examples of Subclasses of Exception class: (possible answers: ClassNotFoundException and IOException) 1. .......: Attempt to use a class that does not exist. This exception would occur, for example, if you tried to run a nonexistent class using the java command, or if your program were composed of, say, three class files, only two of which could be found. 2. .......: Related to input/output operations, such as invalid input, reading past the end of a file, and opening a nonexistent file. Examples of subclasses of IOException are InterruptedIOException, EOFException (EOF is short for End of File), and FileNotFoundException.

ClassNotFoundException, IOException

The ..... class is used to obtain file properties and manipulate files. It does not contain the methods for creating a file or for reading/writing data from/to a file.

File

The ...... class contains the methods for obtaining the properties of a file/directory and for renaming and deleting a file/directory.

File

Suppose we have a method header: "public void myMethod() throws IOException" => The throws keyword indicates that myMethod might throw an ..... If the method might throw multiple exceptions, add a list of the exceptions, separated by ....., after throws If a method does not declare exceptions in the ......, you cannot override it to declare exceptions in the subclass.

IOException, commas, superclass

Examples of Subclasses of Error class: (possible answers: VirtualMachineError and LinkageError) 1. ......: A class has some dependency on another class, but the latter class has changed incompatibly after the compilation of the former class. 2. .......: The JVM is broken or has run out of the resources it needs in order to continue operating.

LinkageError, VirtualMachineError

What RuntimeException will the following program throw, if any? public class Test { public static void main(String[] args) { System.out.println(1.0 / 0); } }

No exception

What RuntimeException will the following program throw, if any? public class Test { public static void main(String[] args) { Object o = null; System.out.println(o.toString()); } }

NullPointerException

Use the Scanner class for reading text data from a file and the ...... class for writing text data to a file.

PrintWriter

What is displayed when running the following program? public class Test { public static void main(String[] args) { try { int[] list = new int[10]; System.out.println("list[10] is " + list[10]); } catch (ArithmeticException ex) { System.out.println("ArithmeticException"); } catch (RuntimeException ex) { System.out.println("RuntimeException"); } catch (Exception ex) { System.out.println("Exception"); } } }

RuntimeException

What is displayed when running the following program? public class Test { public static void main(String[] args) { try { method(); System.out.println("after the method call"); } catch (RuntimeException ex) { System.out.println("RuntimeException in main"); } catch (Exception ex) { System.out.println("Exception in main"); } } static void method() throws Exception { try { String s ="abc"; System.out.println(s.charAt(3)); } catch (RuntimeException ex) { System.out.print("RuntimeEx in method(), "); } catch (Exception ex) { System.out.println("Exception in method()"); } }

RuntimeException in method(), After the method call

You can use ..... to read string and primitive data values from a text file and use ...... to create a file and write data to a text file.

Scanner, PrintWriter

What RuntimeException will the following program throw, if any? public class Test { public static void main(String[] args) { String s = "abc"; System.out.println(s.charAt(3)); } }

StringIndexOutOfBoundsException

The ..... class is the root of exception classes. All Java exception classes inherit directly or indirectly from Throwable. You can create your own exception classes by ..... Exception or a ..... of Exception.

Throwable, extending, subclass

You can read from a file on the Web using the ...... class.

URL

The code in the finally block is executed under .... circumstances, regardless of whether an exception occurs in the try block or is caught

all

Suppose the program can create an instance of "IllegalArgumentException" and throw it. Which one of the following statements is correct? 1. IllegalArgumentException ex = new IllegalArgumentException("Wrong"); throw ex; 2. throw new IllegalArgumentException("Wrong");

both of them

Exception handling enables a method to throw an exception to its .....

caller

The advantage of using exception handling: It enables a method to throw an exception to its caller, enabling the ..... to handle the ...... Without this capability, the called method itself must handle the exception or terminate the program. The key benefit of exception handling is separating the detection of an ..... (done in a called method) from the ...... of an error (done in the calling method).

caller, exception, error, handling

Throwing an exception along with another exception forms a ..... exception.

chained

The following program is not executed: public void m(int value) { if (value < 40) throw new Exception("value is too small"); } => because the method throws a ....... exception. It must be caught or thrown.

checked

When declaring a method, you have to declare a ...... exception if the method might throw it, thus telling the compiler what can go wrong.

checked

A ...... exception must be explicitly declared in the method declaration, if a method throws it. A checked exception must be ...... in a try-catch block. An ...... exception does not need to be declared and does not need to be caught. In Java, the only unchecked exceptions are ...... and ...... and their subclasses.

checked, caught, unchecked, RuntimeException, Error

What is the output of the following code? public class test1 { public static void main(String[] args) { try { int value = 50; if (value < 40) throw new Exception("too small, "); } catch (Exception ex) { System.out.print(ex.getMessage()); } System.out.println("continue"); } }

continue

In Java, the statement currently being executed belongs to a method. The Java interpreter invokes the main method to start executing a program. Every method must state the types of checked exceptions it might throw. This is known as ...... exceptions. To declare an exception in a method, use the throws keyword in the method ....

declaring, header

Java's exception-handling model is based on three operations: ..... an exception, ...... an exception, and ...... an exception

declaring, throwing, catching

Invoking the constructor of PrintWriter will create a new file if the file does not exist. If the file already exists, the current content in the file will be ....... without verifying with the user.

discarded

If you attempt to create a Scanner for a nonexistent file, an ...... will occur. If you attempt to create a PrintWriter for an existing file, the contents of the existing file will be ......

exception, gone

Suppose we have this statement: "throw new ArithmeticException("Divisor cannot be zero");" => The value thrown, in this case new ArithmeticException("Divisor cannot be zero"), is called an ....... The execution of a throw statement is called throwing an exception. The exception is an object created from an exception class. In this case, the exception class is ...... The constructor ArithmeticException(str) is invoked to construct an exception object, where ...... is a message that describes the exception.

exception, java.lang.ArithmeticException, str

Exception handling enables a program to deal with ...... situations and continue its ..... execution.

exceptional, normal

..... are represented in the Exception class, which describes errors caused by your program and by ..... circumstances. These errors can be caught and handled by your program.

exceptions, external

In Java, runtime errors are thrown as ...... An exception is an ..... that represents an error or a condition that prevents execution from proceeding normally. If the exception is not handled, the program will terminate ......

exceptions, object, abnormally

The class names Error, Exception, and RuntimeException are somewhat confusing. All three of these classes are ....., and all of the errors occur at ......

exceptions, runtime

Use ...... in the File class to check whether a file exists. Use ....... in the File class to delete this file. Use ........ to rename the name for this file. Use ....... to return the file size. Use ...... or ..... to create a directory under this File object.

exists(), delete(), renameTo(File), length(), mkdir, mkdirs

When an exception occurs in a method, the method ...... immediately if it does not catch the exception. If the method is required to perform some task before exiting, you can catch the exception in the method and then ...... it to its caller.

exits, rethrow

True or False? We can create a File object using the following statement -> new File("c:\book\test.dat");

false

True or False? "You use the throw statement in the method to throw an exception. You can throw multiple exceptions in a single throw statement."

false

True or False? "we can use the File class for I/O"

false

Occasionally, you may want some code to be executed regardless of whether an exception occurs or is caught. Java has a ..... clause that can be used to accomplish this objective

finally

The code in the ...... block is executed under all circumstances, regardless of whether an exception occurs in the try block or whether an exception is caught if it occurs.

finally

The order in which exceptions are specified in catch blocks is ....... A compile error will result if a catch block for a ...... type appears before a catch block for a ..... type.

important, superclass, subclass

A program that detects an error can create an ........ of an appropriate exception type and throw it. This is known as throwing an exception.

instance

Java forces you to deal with checked exceptions. If a method declares a checked exception (i.e., an exception other than Error or RuntimeException), => you must ...... it in a try-catch block or ....... to throw the exception in the calling method.

invoke, declare

If an exception is not caught in the current method, it is passed to its caller. The process is repeated until the exception is caught or passed to the ...... method.

main

Can the following program be executed? public void m(int value) { if (value < 40) throw new Exception("value is too small"); }

no

Does the code throw any exceptions? long value = Long.MAX_VALUE + 1; System.out.println(value);

no

Does the presence of a try-catch block impose overhead when no exception occurs?

no

Suppose that statement2 causes an exception in the following try-catch block: try { statement1; statement2; statement3; } catch (Exception1 ex1) { } catch (Exception2 ex2) { } statement4; => If the exception is not caught, will statement4 be executed?

no

Suppose that statement2 causes an exception in the following try-catch block: try { statement1; statement2; statement3; } catch (Exception1 ex1) { } catch (Exception2 ex2) { } statement4; => Will statement3 be executed?

no

Suppose that statement2 may cause an exception in the following code: try { statement1; statement2; statement3; } catch (Exception1 ex1) { } catch (Exception2 ex2) { throw ex2; } finally { statement4; } statement5; If the exception is not Exception1 nor Exception2, will statement4 be executed, and will statement5 be executed?

only statement4

Suppose that statement2 may cause an exception in the following code: try { statement1; statement2; statement3; } catch (Exception1 ex1) { } catch (Exception2 ex2) { throw ex2; } finally { statement4; } statement5; If the exception is of type Exception2, will statement4 be executed, and will statement5 be executed?

only statement4

The File class contains the methods for obtaining file and directory properties and for renaming and deleting files and directories. However, the File class does not contain the methods for ...... and ..... file contents.

reading, writing

If one of the statements inside the try block throws an exception, Java skips the ..... statements in the try block and starts the process of finding the code to handle the exception. The code that handles the exception is called the exception ......; it is found by propagating the exception backward through a chain of method calls, starting from the current method. Each catch block is examined in turn, from .... to ....., to see whether the type of the exception object is an instance of the exception class in the catch block. If so, the exception object is assigned to the variable declared, and the code in the catch block is executed. If no handler is found, Java exits this method, passes the ..... to the method that invoked the method, and continues the same process to find a handler. If no handler is found in the chain of methods being invoked, the program ...... and prints an error message on the ..... The process of finding a handler is called ...... an exception.

remaining, handler, first, last, exception, terminates, console, catching

Java allows an exception handler to ..... the exception if the handler cannot process the exception or simply wants to let its caller be notified of the exception.

rethrow

The Throwable class is the ..... of Java exception classes. Error and Exception are subclasses of Throwable. Error describes fatal ..... errors, and Exception describes the errors that can be handled by Java ...... The subclasses of .... are LinkageError, VirtualMachineError, and AWTError. The subclasses of ...... include RuntimeException, IOException, AWTException, and InstantiationException.

root, system, programs, Error, Exception

........ are represented in the RuntimeException class, which describes ....... errors, such as bad casting, accessing an out-of-bounds array, and numeric errors. Runtime exceptions are generally thrown by the ......

runtime exceptions, programming, JVM

..... errors occur while a program is running if the JVM detects an operation that is impossible to carry out. For example, if you access an array using an index that is out of bounds, you will get a runtime error with an ...... If you enter a double value when your program expects an integer, you will get a runtime error with an .......

runtime, ArrayIndexOutOfBoundsException, InputMismatchException

The purpose of declaring exceptions is to tell the Java ...... system what can go wrong. You declare an exception using the ...... keyword in the method declaration. You can declare multiple exceptions, separated by .......

runtime, throws, commas

Exception handling ....... error-handling code from normal programming tasks, thus making programs easier to read and to modify.

separates

In the java.lang.Throwable class, the getMessage() is defined in the Throwable class to return a ....... that describes the exception.

string

The file name is a ....... The File class is a wrapper class for the file name and its directory path. For example, new File("c:\\book") ..... a File object for the directory c:\book, and new File("c:\\book\\test.dat") creates a File object for the file c:\book\test.dat,

string, creates

Various exception classes can be derived from a common ....... If a catch block catches the exception objects of a superclass, it can also catch all the exception objects of the ...... of that superclass.

superclass, subclasses

The exception classes can be classified into three major types: ......., ......, and .......

system errors, exceptions, runtime exceptions

...... are thrown by the JVM and are represented in the Error class. The Error class describes ..... system errors, though such errors rarely occur. If one does, there is little you can do beyond notifying the user and trying to terminate the program gracefully.

system errors, internal

Suppose we have the following statements: public static int quotient(int number1, int number2) { if (number2 == 0) throw new ArithmeticException("Error2:"); return number1 / number2; } public static void main(String[] args) { try { int result = quotient(number1, number2); System.out.println(number1); } catch (ArithmeticException ex) { System.out.println("Error!!!"; } } => An exception may be thrown directly by using a ...... statement in a try block, or by invoking a ..... that may throw an exception. The main method invokes "quotient" method. If the quotient method executes normally, it returns a value to the caller. If the quotient method encounters an exception, it throws the exception back to its ....... The caller's ..... block handles the exception.

throw, method, caller, catch

throw is for ....... exceptions and throws is for ...... (or declaring) exceptions.

throwing, claiming

The keyword to declare an exception is ....., and the keyword to throw an exception is .......

throws, throw

What is the output of the following code? public class test1 { public static void main(String[] args) { try { int value = 30; if (value < 40) throw new Exception("too small, "); } catch (Exception ex) { System.out.print(ex.getMessage()); } System.out.println("continue"); } }

too small, continue

In the java.lang.Throwable class, the method printStackTrace() displays ...... information to the console.

trace

True of False? "Exceptions are objects, and objects are defined using classes. The root class for exceptions is java.lang.Throwable."

true

True of False? "If a method does not declare exceptions in the superclass, you cannot override it to declare exceptions in the subclass."

true

True or False? The statement "new Scanner(new File(filename))" reads data from a file

true

True or False? "A handler for an exception is found by propagating the exception backward through a chain of method calls, starting from the current method."

true

True or False? "Constructing a File instance does not create a file on the machine. You can create a File instance for any file name regardless whether it exists or not. You can invoke the exists() method on a File instance to check whether the file exists."

true

True or False? "Create a URL object and use new Scanner(url.openStream()) to create a scanner object for reading data from the URL stream."

true

True or False? "Exceptions are thrown from a method. The caller of the method can catch and handle the exception."

true

True or False? "Invoking the constructor of PrintWriter may throw an I/O exception. "

true

True or False? "The File class can be used to obtain file properties and manipulate files, but cannot perform I/O. No. Creating a File object does not create a file/directory on the disk."

true

True or False? "The catch block may be omitted when the finally clause is used."

true

True or False? "The directory separator for Windows is a backslash (\)."

true

True or False? "To create a PrintWriter for a file, use new PrintWriter(filename)"

true

True or False? "You can define a custom exception class by extending the java.lang.Exception class."

true

True or False? "You use the throw statement in the method to throw an exception. You cannot throw multiple exceptions in a single throw statement."

true

True or False? "in Java, you can read data from a file on the Web."

true

Suppose we have the following statements: public static int quotient(int number1, int number2) { if (number2 == 0) throw new ArithmeticException("Error2:"); return number1 / number2; } public static void main(String[] args) { try { int result = quotient(number1, number2); System.out.println(number1); } catch (ArithmeticException ex) { System.out.println("Error!!!"; } } => The ....... block contains the code that is executed in normal circumstances. The exception is caught by the ...... block. The code in the catch block is executed to handle the ....... The throw statement is analogous to a method call, but instead of calling a method, it calls a ..... block. In this sense, a catch block is like a method definition with a parameter that matches the type of the value being thrown. Unlike a method, however, after the catch block is executed, the program control does not return to the ...... statement; instead, it executes the next ..... after the catch block. An exception may be thrown directly by using a ...... statement in a try block, or by invoking a ..... that may throw an exception. The main method invokes "quotient" method. If the quotient method executes normally, it returns a value to the caller. If the quotient method encounters an exception, it throws the exception back to its ....... The caller's catch block handles the exception.

try, catch, exception, catch, throw, statement

IllegalArgumentException is an exception class in the Java API. In general, each exception class in the Java API has at least ..... constructors: a no-arg constructor, and a constructor with a ..... argument that describes the exception. This argument is called the exception message, which can be obtained using ............

two, String, getMessage()

In most cases, ...... exceptions reflect programming logic errors that are unrecoverable. These are logic errors that should be corrected in the program. Unchecked exceptions can occur ...... in a program

unchecked, anywhere

Exceptions occur during the execution of a method. RuntimeException and Error are ...... exceptions; all other exceptions are .......

unchecked, checked

RuntimeException, Error, and their subclasses are known as ...... exceptions. All other exceptions are known as ...... exceptions, meaning that the compiler forces the programmer to check and deal with them in a ...... block or declare it in the method header

unchecked, checked, try-catch

When should you use a try-catch block in the code? => Use it when you have to deal with ..... error conditions. Do not use a try-catch block to deal with simple, expected situations.

unexpected

You can use the new JDK 7 multi-catch feature to simplify coding for the exceptions with the same handling code. Each exception type is separated from the next with a ....... (|). If one of the exceptions is caught, the handling code is executed.

vertical bar

Can the following program be executed? public void m(int value) throws Exception { if (value < 40) throw new Exception("value is too small"); }

yes

Suppose that statement2 causes an exception in the following try-catch block: try { statement1; statement2; statement3; } catch (Exception1 ex1) { } catch (Exception2 ex2) { } statement4; => If the exception is caught in the catch block, will statement4 be executed?

yes

Suppose that statement2 may cause an exception in the following code: try { statement1; statement2; statement3; } catch (Exception1 ex1) { } catch (Exception2 ex2) { throw ex2; } finally { statement4; } statement5; If no exception occurs, will statement4 be executed, and will statement5 be executed?

yes, both

Suppose that statement2 may cause an exception in the following code: try { statement1; statement2; statement3; } catch (Exception1 ex1) { } catch (Exception2 ex2) { throw ex2; } finally { statement4; } statement5; If the exception is of type Exception1, will statement4 be executed, and will statement5 be executed?

yes, both


संबंधित स्टडी सेट्स

Chapter 14 Automating Customized Attacks

View Set

Financial Accounting Ch.4 Part 4

View Set

translation and regulation of gene expression

View Set

Kvintagymji - chemie - jádro atomu

View Set

Maine Insurance - Questions Frequently Missed

View Set