Java For Android Tutorial #13: Handling multiple exceptions

Code:

package com.example;
public class java {
    public static void main(String[] str) {
        String denom = "s";
        try {
            int denominator = Integer.parseInt(denom);
            int numerator = 231;
            int remainder = numerator % denominator;
            if (remainder == 0) {
                System.out.println(denominator + " is a factor of " + numerator);
            }

        }
        catch (NumberFormatException e){
            System.out.println(denom + " is not an integer");
        }
        catch (ArithmeticException e){
            System.out.println("You can't divide any number by 0");
        }
    }
}

Programming languages such as Java allows programmers to handle multiple exceptions without worrying about handling different type of exceptions. In this program, I have taken 2 types of exceptions such as arithmetic and number format exception. My purpose is to explain in the process in simple words. 

Comments

Popular Posts