write a program implement switch block inside try block switch block must contains four different case values each generating exception. all the four car of switch block must be execute when program execute and an the exception raised must be handled by a single catch block in java
Answers
Answer:
A Java exception is an object that describes an exceptional (that is, error) condition that has occurred in a piece of code. When an exceptional condition arises, an object representing that exception is created and thrown in the method that caused the error. That method may choose to handle the exception itself or pass it on. Either way, at some point, the exception is caught and processed.
The programs you write can generate many types of potential exceptions, such as when you do the following:
In Java there are three types of loops:
You issue a command to read a file from a disk, but the file does not exist there.
You attempt to write data to a disk, but the disk is full or unformatted.
Your program asks for user input, but the user enters invalid data.
The program attempts to divide a value by 0, access an array with a subscript that is too large or calculate a value that is too large for the answer’s variable type.
These errors are called exceptions because, presumably, they are not usual occurrences; they are “exceptional.” The object-oriented techniques to manage such errors comprise the group of methods known as exception handling.
Exception handling works by transferring the execution of a program to an appropriate exception handler when an exception occurs. Let’s take an example program which will do take two numbers from user and print division result on screen. This might lead to exception condition if the denominator is zero.
Java Code: Go to the editor
import java.util.Scanner;
public class DivideExceptionDemo {
public static void main(String[] args) {
//Scanner class is wrapper class of System.in object
Scanner inputDevice = new Scanner(System.in);
System.out.print("Please enter first number(numerator): ");
int numerator = inputDevice.nextInt();
System.out.print("Please enter second number(denominator): ");
int denominator = inputDevice.nextInt();
new DivideExceptionDemo().doDivide(numerator, denominator);
}
public void doDivide(int a, int b){
float result = a/b;
System.out.println("Division result of "+ a +"/"+b +"= " +result);
}
}