COSC-1337 Chapter 11 Assignment
If, within one try statement you want to have catch clauses of the following types, in which order should they appear in your program: (1) Exception (2) IllegalArgumentException (3) RuntimeException (4) Throwable
2,3,1,4
In the following code, assume that inputFile references a Scanner object that has been successfully used to open a file: double totalIncome = 0.0; while (inputFile.hasNext()) { try { totalIncome += inputFile.nextDouble(); } catch(InputMismatchException e) { System.out.println("Non-numeric data encountered " + "in the file."); inputFile.nextLine(); } finally { totalIncome = 35.5; } } What will be the value of totalIncome after the following values are read from the file?
35.5
If you want to append data to the existing binary file, BinaryFile.dat, write two statements to open the file.
FileOutputStream fstream = new FileOutputStream("BinaryFile.dat", true); DataOutputStream binaryOutputFile = new DataOutputStream(fstream);
What is meant when it is said that an exception is thrown?
an exception object has been created in response to an error that has occurred
Why does the following code cause a compiler error? try { number = Integer.parseInt(str); } catch (IllegalArgumentException e) { System.out.println("Bad number format."); } catch (NumberFormatException e) { System.out.println(str + " is not a number."); }
because NumberFormatException runs after IllegalArgumentException
If your code does not handle and exception when it is thrown, _________ prints an error message and crashes the program
default exception handler
The ability to catch multiple types of exceptions with a single catch is known as ________, and was introduced in Java 7.
multi catch
what does it mean to catch an exception?
program intercepts the exception and responds to it
Find the Error // Assume inputFile references a Scanner object try { input = inputFile.nextInt(); } finally { inputFile.close(); } catch (InputMismatchException e) { System.out.println (e.getMessage()); }
the finally block should be at the end
Find the Error catch (FileNotFoundException e) { catch (FileNotFoundException e) { System.out.println ("File not found."); } try { File file = new File ("MyFile.txt"); Scanner inputFile = new Scanner (file); }
the try block is first