In: Computer Science
1. What is exception handling?
2. Name a few common types of exceptions.
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