COSC-1337 Chapter 11 Assignment

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

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


Kaugnay na mga set ng pag-aaral

Ch 3: The Accounting Cycle: End of the Period

View Set

Exam FX Life and Health Insurance Practice Exam: Learning Mode

View Set

Functions in Python: Working with Advanced Features of Python Functions

View Set

Chapter 57 The Child With Alterations in Skin Integrity

View Set

Econ 202 Homework for Chapter 14

View Set

MGMT 329 CH 4 EMPLOYEE RIGHTS AND DISCIPLINE 4.1-4.7

View Set

American History II - Unit XI - The Modern Era (90s-Present)

View Set

AP Psychology Chapter 4: The Developing Person

View Set