Question

Explain the Two types of exceptions checked and unchecked with examples.

Explain the Two types of exceptions checked and unchecked with examples.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Object Throwable Exception Error VirtualMachineError IOException Assertiontrror SQLException RuntimeException ArithmeticExcep

Checked exceptions are the exceptions that are checked at compile time. These type of exception requires being caught and handled during compile time.

  • If compiler does not see try or catch block handling a Checked Exception, it throws Compilation error.

Checked Exception in Java – All exceptions which are direct sub-class of Exception class but does not inherit RuntimeException are Checked Exception

When to use Checked Exception?

  • Operations where chances of failure are more
  • What to do when exception occurs – part of Business process
  • Reminder to handle a failed scenario

Some Examples of Checked Exceptions in Java – IOException, SQLException, DataAccessException, ClassNotFoundException, FileNotFoundException

Code Example: A very common example is the FileNotFoundException

import java.io.*;

class Main {

    public static void main(String[] args) {

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

        BufferedReader fileInput = new BufferedReader(file);

         

        // Print first line of file "C:\test\test.txt"

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

         

        fileInput.close();

    }

}

Result: This program does not compiles and throws a FileNotFoundException (checked exception).

Exception in thread "main" java.lang.Error: Unresolved compilation problems:

       Unhandled exception type FileNotFoundException

       Unhandled exception type IOException

       Unhandled exception type IOException

       at test.Main.main(Main.java:7)

Point to Note: 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.

Unchecked Exceptions 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.
  • In Java exceptions under RuntimeException classes are unchecked exceptions.

Some Examples of Unchecked Exceptions in Java: Arithmetic Exception, NullPointerException

Code Example: 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;

  }

}

Result:

Exception in thread "main" java.lang.ArithmeticException: / by zero

       at test.Main.main(Main.java:7)

Add a comment
Know the answer?
Add Answer to:
Explain the Two types of exceptions checked and unchecked with examples.
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT