package Practice.test2; import java.util.InputMismatchException; import java.util.Scanner; public class ExceptionalCalculator { private double result, num1, num2; public ExceptionalCalculator(double num1, double num2){ this.num1 = num1; this.num2 = num2; } public double add() throws IllegalArgumentException{ if (num1 < 0 || num2 < 0){ throw new IllegalArgumentException("Numbers cannot be negative"); } return num1 + num2; } public double div() throws ArithmeticException{ if (num1 == 0 || num2 == 0){ throw new ArithmeticException("Numbers cannot be zero"); } return num1 / num2; } public String toString(){ return "The numbers are " + num1 + " and " + num2 + " and the result is " + result + "."; } public static void main(String[] args) { // ExceptionalCalculator calc = new ExceptionalCalculator(5, 0); // try { // System.out.println(calc.add()); // } catch (IllegalArgumentException e) { // System.out.println(e.getMessage()); // } // // try{ // System.out.println(calc.div()); // } // catch (ArithmeticException e){ // System.out.println(e.getMessage()); // } // finally{ // System.out.println("Program ended"); // System.out.print(calc); // } Scanner sc = new Scanner(System.in); try{ int num = sc.nextInt(); if (num < 0){ throw new Exception("Number cannot be negative"); } } catch (Exception e){ System.out.println(e.getMessage()); } finally{ System.out.println("Program ended"); } } }