Create a Java program to demonstrate method overloading with overloaded methods that use different parameter types and handle exceptions
public class OverloadingWithExceptionHandlingDemo { public static void main(String[] args) { MethodOverloadingDemo calculator = new MethodOverloadingDemo(); try { int intResult = calculator.divide(10, 2); double doubleResult = calculator.divide(7.5, 2.5); System.out.println("Result of integer division : " + intResult); System.out.println("Result of double division : " + doubleResult); // Attempt to divide by zero intResult = calculator.divide(5, 0); // This will throw an exception } catch (IllegalArgumentException e) { System.err.println("Exception : " + e.getMessage()); } } } class MethodOverloadingDemo { int divide(int a, int b)// Method to divide two integers { if (b == 0) { throw new IllegalArgumentException("Cannot divide by zero"); } return a / b; } double divide(double a, double b)// Method to divide two doubles { if (b == 0.0) { throw new IllegalArgumentException("Cannot divide by zero"); } return a / b; } }
Result of integer division : 5 Result of double division : 3.0 Exception : Cannot divide by zero
Learn All in Tamil © Designed & Developed By Tutor Joes | Privacy Policy | Terms & Conditions