Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Vocabulary: Identify the vocabulary word for each definition below. Try It/Solve

ID: 3857576 • Letter: V

Question

Vocabulary:
Identify the vocabulary word for each definition below.


Try It/Solve It:
1. You would like to write a program that will open a file called “myFile.txt”. Write a try catch statement to open the file
and catch the error if the file fails to open.
2. Create an exception called “myException” that prints out an error message when thrown.
3. Create a block of code that utilizes all three types of invariants and asserts their values

1)A certain kind of try statement that contains resources.------------------? 2)An invariant used to evaluate the assumptions of the class instances-------------?. 3)Certain types of boolean statements that allow you to test specific aspects of your
code.-------------------? 4)Key statement for handling exceptions in Java.-----------? 5)An invariant that handles boolean statements to test internal values.------------? 6)An invariant that handles conditions in control flow statements.------------? 7)A statement that allows you to handle multiple exceptions.----------------? 8)An optional addition to a try-catch statement that will always be executed.-------------? 9)Run-time errors that can be handled inside the program-----------?.

Explanation / Answer

Vocabulary

1) try-with-resources

2) class invariant

3) Conditional statements

4) try and catch block

5) Internal Invariants

6) decision-making statements (if-then, if-then-else, switch)

7) catch (Exception1 | Exception2 ex)

8) finally statement

9) exceptions

1)

package net.codejava.io;

import java.io.FileReader;

import java.io.IOException;

public class TextFileReadingExample1 {

public static void main(String[] args) {

try {

FileReader file_reader = new FileReader("MyFile.txt");

int characters;

while ((characters = file_reader.read()) != -1) {

System.out.print((char) characters);

}

file_reader.close();

} catch (IOException e) {

e.printStackTrace();

}

}

}

2

public static void main(String[] args) { throw new myException("this is the message"); }