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

Exceptions and Assertions Practice Activities Lesson Objectives: Use exception h

ID: 3851985 • Letter: E

Question

Exceptions and Assertions Practice Activities Lesson Objectives: Use exception handling syntax to create reliable applications Recognize common exception classes and categories Create custom exception and auto-closeable resources Test invariants by using assertions Use try and throw statements Use the catch, multi-catch, and finally statements Vocabulary: Identify the vocabulary word for each definition below. Try It/Solve It: 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. Create an exception called "myException" that prints out an error message when thrown. Create a block of code that utilizes all three types of invariants and asserts their values.

Explanation / Answer

1.catch,finally
2.Assertion
3.true,false,0,1;
4.Try,catch,Finally,Throw,Throws
5.if(Boolean value)
6.If,else
7.multi catch.
8.Finally
9.Errors

Exceptionclass.java

package venkanna;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.nio.file.*;
public class Exceptionclass
{
   static BufferedReader br = null;
   static FileReader fr = null;
   private static void myTest() throws MyAppException, IOException
   {
       fr = new FileReader("D:\New folder\myfile.txt");
       br = new BufferedReader(fr);
       String Line;
       br = new BufferedReader(new FileReader("D:\New folder\myfile.txt"));
       Path path = Paths.get("D:\New folder\myfile.txt");
       while ((Line = br.readLine()) != null)
       {
           System.out.println(Line);
       }
       if(!Files.exists(path))
{
throw new MyAppException("The File is Not Found");
}
      
   }
   public static void main(String[] args) throws MyAppException
   {
      
       try
       {
           myTest();  
       }
       catch (MyAppException msg)
       {
           System.out.println("Inside catch block: "+msg.getMessage());
       }
       catch (FileNotFoundException e)
       {
           e.printStackTrace();
       }
       catch (IOException e)
       {
           e.printStackTrace();
       }
       finally
       {
           try
           {
               if (br != null)
                   br.close();
               if (fr != null)
                   fr.close();
           }
           catch (IOException ex)
           {
               ex.printStackTrace();
           }

       }

   }
  

}