Question

In: Computer Science

Exception Handling in Java In addition to what we have learned from the course materials, there...

Exception Handling in Java

In addition to what we have learned from the course materials, there are other ways to categorize exceptions in Java such as checked and unchecked exceptions. Please conduct online research on these two categories, share what you find. Of these two kinds of exceptions (checked and unchecked), which method do you prefer? Which method is easier?

Solutions

Expert Solution

Checked Exception:-

Checked exceptions are checked at compile-time. It means if a method is throwing a checked exception then it should handle the exception using try-catch block or it should declare the exception using throws keyword, otherwise the program will give a compilation error...

Example:-

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:\test\a.txt"

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

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

         

        fileInput.close();

    }

}

Unchecked Exception:-

Unchecked exceptions are not checked at compile time. It means if your program is throwing an unchecked exception and even if you didn’t handle/declare that exception, the program won’t give a compilation error. Most of the times these exception occurs due to the bad data provided by user during the user-program interaction. It is up to the programmer to judge the conditions in advance, that can cause such exceptions and handle them appropriately. All Unchecked exceptions are direct sub classes of RuntimeException class..

class Example {  
   public static void main(String args[])
   {
        int num1=10;
        int num2=0;
        /*Since I'm dividing an integer with 0
         * it should throw ArithmeticException
         */
        int res=num1/num2;
        System.out.println(res);
   }
}

I would prefer unchecked exception because Checked Exceptions are used for predictable, but unpreventable errors that are reasonable to recover from whereas Unchecked Exceptions are used for everything else.

If a client can reasonably be expected to recover from an exception, make it a checked exception. If a client cannot do anything to recover from the exception, make it an unchecked exception..


Related Solutions

in C#, What is an exception and what are the advantages of exception handling?
in C#, What is an exception and what are the advantages of exception handling? What are the differences between the traditional error-handling methods and the object-oriented exception-handling methods and when should you use each one? Provide three to four paragraphs detailing your findings/views on these questions..provide examples with a simple code also..
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a  ...
*In Java please! EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates   an   exception   class   called   ManyCharactersException,   designed   to   be   thrown   when   a   string   is   discovered   that   has   too   many   characters   in   it. Consider   a   program   that   takes   in   the   last   names   of   users.   The   driver   class   of   the   program reads the   names   (strings) from   the   user   until   the   user   enters   "DONE".   If   a   name is   entered   that   has   more   than   20   characters,  ...
We have learned from this course that the real value of the debt is eroded by...
We have learned from this course that the real value of the debt is eroded by inflation and may be overestimated because of it and other factors. Discuss whether you feel that the debt will be a major concern during your working lifetime and retirement why or why not?                             
Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage(...
Exception handling All Exceptions that can be thrown must descend from this Java class: The getMessage( ) method is inherited from this class? What are the two broad catagories of Exceptions in Java? If an Exception is not a checked exception it must extend what class? What is the difference between a checked Exception and an unchecked Exception? What are the two options a programmer has when writing code that may cause a checked Exception to be thrown? Can a...
In 2 paragraphs please tell me what you have learned from this course. The course is...
In 2 paragraphs please tell me what you have learned from this course. The course is Managed Care Policies and Implications.
(COURSE) MKTG1370 Summarize what you have learned in this course.
(COURSE) MKTG1370 Summarize what you have learned in this course.
what are the lessons learned from sociology course?
what are the lessons learned from sociology course?
Discuss"Is the Customer Always Right?" (COURSE) MKTG1160 Summarize what you have learned from this course and...
Discuss"Is the Customer Always Right?" (COURSE) MKTG1160 Summarize what you have learned from this course and how you will apply it in the future.
Create a java program that: - Has a class that defines an exception -Have that exception...
Create a java program that: - Has a class that defines an exception -Have that exception throw(n) in one method, and be caught and handled in another one. -Has a program that always continues even if incorrect data is entered by the user -has a minimum of 2 classes in it
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define...
*IN JAVA* EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2. Define an exception class and call it from the driver program. For this lab you will complete two parts. Part (a) of the lab implements try… catch and uses an existing exception in C# or in Java. Write a program that implements an ArrayIndexOutOfBounds error. Part (b) writes a program that converts a time from 24-hour notation to 12-hour notation. Assume the user will enter...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT