Write a Java program to demonstrate method overloading with methods that have different exception types
public class Main { public static void main(String[] args) { Calculator calculator = new Calculator(); System.out.println("Result of division : " + calculator.divide(10, 2)); try { System.out.println("Result of division (with exception) : " + calculator.divide(10, 5, 0)); } catch (ArithmeticException e) { System.out.println("Exception : " + e.getMessage()); } } } class Calculator { int divide(int a, int b) { return a / b; } int divide(int a, int b, int c) throws ArithmeticException { if (c == 0) { throw new ArithmeticException("Division by zero"); } return (a / b) / c; } }
Result of division : 5 Exception : Division by zero
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions