Question

In: Computer Science

1. What is exception handling? 2. Name a few common types of exceptions.

1. What is exception handling?

2. Name a few common types of exceptions.

Solutions

Expert Solution

Note: Could you plz go through this let me know if u need any further clarity in this.Thank You
_________________

1)

Exception : This is an unwanted event which occurred during program run time which disrupts normal flow of program execution.

Exception handling is a mechanism which is used to handle the run time exceptions.

Generally we use try catch block to handle the unexpected exceptions raised during runtime of the program.

We write all the statements in the try block where there is a possibility of raising exceptional circumstances.

If an exception is raised if we didnt caught that exception then the program will terminate abruptly.

To catch the exception we have to use the catch block.Catch block requires a parameter that should be of type Exception.

We can have multiple catch blocks with a try block.

// Try- catch Block format:

try

{

// Programming logic

}

catch(Exception e)

{

// Block of code to handle the errors

}

_______________________

2)

We have many run time errors.Some of them are :

1) Arithmetic Exception : This will occur during performing any mathematical exceptions like divided by 0.

2) NumberFormat Exception : This will occur when we are trying to convert a String into an numeric value.

3) ArrayIndexOutOfBounds Exception : This will occur if we are trying to access the array element which is beyond the array size.

4) NullPointer Exception : This will occur if we are trying to access an object using the object reference, but the reference is pointing to null.

___________________________

// Demo for Input Mismatch Exception :

1)

// InputMismatchExceptionDemo.java

import java.util.InputMismatchException;
import java.util.Scanner;

public class InputMismatchExceptionDemo {

   public static void main(String[] args) {

       // Declaring variables
       double a, b, sum = 0;

       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       while (true) {
           try {
               // Getting the input entered by the user
               System.out.print("Enter Number a:");
               a = sc.nextDouble();
               System.out.print("Enter Number b:");
               b = sc.nextDouble();
               sum = a + b;
               System.out.println("Sum of " + a + " abd " + b + " is :" + sum);
               break;
           } catch (InputMismatchException e) {
               sc.nextLine();
               System.out.println("Exception :"+e);
              
               continue;
           }
       }

   }

}
____________________

Output:

Enter Number a:3.4
Enter Number b:hello
Exception :java.util.InputMismatchException
Enter Number a:4.5
Enter Number b:5.5
Sum of 4.5 abd 5.5 is :10.0

_____________________

2)

// Demo for ArrayIndexOutOfBoundsException :

// ArrayIndexOutOfBoundsExceptionDemo.java

import java.util.Random;
import java.util.Scanner;

public class ArrayIndexOutOfBoundsExceptionDemo {

   public static void main(String[] args) {
       int indx;
       int arr[] = new int[100];

       /*
       * Creating an Scanner class object which is used to get the inputs
       * entered by the user
       */
       Scanner sc = new Scanner(System.in);

       // Creating an random class object
       Random r = new Random();
       for (int i = 0; i < arr.length; i++) {
           arr[i] = r.nextInt(100) + 1;
       }

       while (true) {
           try {
               System.out.print("Enter Index to search :");
               indx = sc.nextInt();

               System.out.println("The number present at index " + indx
                       + " is :" + arr[indx]);

               break;
           } catch (ArrayIndexOutOfBoundsException e) {
               System.out.println("Exception :" + e);
               continue;
           }
       }
   }

}
________________________

Output:

Enter Index to search :105
Exception :java.lang.ArrayIndexOutOfBoundsException: 105
Enter Index to search :98
The number present at index 98 is :19

_______________Could you plz rate me well.Thank You


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..
EXCEPTION HANDLING Concept Summary: 1. Exception handling 2. Class design Description Write a program that creates...
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, throw the exception....
*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,  ...
EXCEPTION HANDLING Concept Summary: 1. Exception   handling   2. Class   design Description Write   a   program   that   creates  ...
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,   throw   the   exception.  ...
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...
How does try/catch(exception) processing help resolve run time exceptions? What is a custom exception? What are...
How does try/catch(exception) processing help resolve run time exceptions? What is a custom exception? What are it's benefits? What does it mean when we raise a custom exception?
*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...
(in C# please.) EXCEPTION HANDLING Concept Summary: 1. Use of try… catch in a program 2....
(in C# please.) 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...
1) What are a few (name at least three) important values in Japanese culture? 2) What...
1) What are a few (name at least three) important values in Japanese culture? 2) What are the norms? 3) What does the article say about religion in Japan? 4) How is Japanese culture different from American culture? 5) How is Japanese culture similar to American culture?
2. Name 2 types of water pollution (not sedimentation) and provide a common source (cause) for...
2. Name 2 types of water pollution (not sedimentation) and provide a common source (cause) for each. 1) 2)
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT