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

1. A) What is the difference of Checked Exception and Unchecked Exception B) Wil

ID: 3913923 • Letter: 1

Question

1. A) What is the difference of Checked Exception and Unchecked Exception B) Will the code below compile or not? Why? No 2. public class Eagle public static void gulgate) if (today"Thursday") throw new IOException("hi"); 2. Access control allows us to restrict the use of fields, methods, and classes. public: Accessible by everyone. protected: Accessible by the class itself, the package, and any subclasses. default (no modifier): Accessible by the class itself and the package. private: Accessible only by the class itself. A class is immutable if nothing about its instances can change after they are constructed. Which of the following classes are immutable and why? (Explain your answer) 1 public class Pebble f public int weight; public Pebble() { weight-1; } 1 public class Rock becpae nsne 2 public final int weight; 3 public Rock (int w) weight w;

Explanation / Answer

1)A)Answer:

1) Checked Exception

The classes that extend Throwable class except RuntimeException and Error are known as checked exceptions e.g.IOException, SQLException etc. Checked exceptions are checked at compile-time.

) Checked: are the exceptions that are checked at compile time. If some code within a method throws a checked exception, then the method must either handle the exception or it must specify the exception using throws keyword.

For example, consider the following Java program that opens file at locatiobn “C: est.txt” and prints first three lines of it. The program doesn’t compile, because the function main() uses FileReader() and FileReader() throws a checked exception FileNotFoundException. It also uses readLine() and close() methods, and these methods also throw checked exception IOException

import java.io.*;

class Main {

    public static void main(String[] args) {

        FileReader file = new FileReader("C:\test\a.txt");

        BufferedReader fileInput = new BufferedReader(file);

         

        // Print first 3 lines of file "C: est.txt"

        for (int counter = 0; counter < 3; counter++)

            System.out.println(fileInput.readLine());

         

        fileInput.close();

    }

}

Output:

2) Unchecked Exception

The classes that extend RuntimeException are known as unchecked exceptions e.g. ArithmeticException, NullPointerException, ArrayIndexOutOfBoundsException etc. Unchecked exceptions are not checked at compile-time rather they are checked at runtime

Unchecked are the exceptions that are not checked at compiled time. In C++, all exceptions are unchecked, so it is not forced by the compiler to either handle or specify the exception. It is up to the programmers to be civilized, and specify or catch the exceptions.
In Java exceptions under Error and RuntimeException classes are unchecked exceptions, everything else under throwable is checked.

Consider the following Java program. It compiles fine, but it throws ArithmeticException when run. The compiler allows it to compile, because ArithmeticException is an unchecked exception.

class Main {

   public static void main(String args[]) {

      int x = 0;

      int y = 10;

      int z = y/x;

  }

}

Run on IDE

Output:

import java.io.*;

class Main {

    public static void main(String[] args) {

        FileReader file = new FileReader("C:\test\a.txt");

        BufferedReader fileInput = new BufferedReader(file);

         

        // Print first 3 lines of file "C: est.txt"

        for (int counter = 0; counter < 3; counter++)

            System.out.println(fileInput.readLine());

         

        fileInput.close();

    }

}

Output:

  Exception in thread "main" java.lang.RuntimeException: Uncompilable source code -   unreported exception java.io.FileNotFoundException; must be caught or declared to be   thrown          at Main.main(Main.java:5)