Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package javaPack;
- import java.util.InputMismatchException;
- import java.util.Scanner;
- public class Exceptionhandling {
- public static void main(String[] args) {
- int number1 = 0, number2 = 0, result =0;
- boolean isFlag = false;
- do {
- try {
- Scanner input = new Scanner(System.in);
- System.out.println("Enter first integer no: ");
- number1 = input.nextInt();
- System.out.println("Enter second integer no: ");
- number2 = input.nextInt();
- result = number1/number2;
- System.out.println("Result: " + result);
- isFlag = true;
- input.close();
- }
- //All types of exception can be named as e in each catch
- catch(InputMismatchException e) {
- System.out.println("Please enter valid number!");
- }
- catch(ArithmeticException e) {
- System.out.println("Second number cannot be zero!");
- }
- catch(Exception e) {
- System.out.println("An exception occurred");
- }
- finally {
- System.out.println("This statement is always excuted.");
- }
- } while(!isFlag); //Loop will keep going when exception occurs
- //until when correct output is shown then isFlag will become true
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement