'Exception Handling using a Constructor (JAVA)

I'm currently working on a project in which I am to ask the user to input 2 numbers, divide them, and then throw an exception if the denominator is 0.

I feel like I'm close but I can figure out how to get the sysout from the constructor to print when the exception is thrown. Also, the instructions I'm following state I "must" add a try/catch in order to call the divide method.

Within the code I've added comments with numbered instructions I'm following. I've been trying to follow them to the best of my abilities, but I can't figure out:

  1. How to get constructor to throw the exception
  2. How to use a try/catch to call the method.

I have a feeling I'm supposed to be using a subclass to access the constructor.

Any help is much appreciated!

Here is the Exception class:

public class DivisionException {
    
    
    public DivisionException () {

        System.out.println("Error: you cannot divide by zero");
        
    }
    
}

And here is the Main class:

import java.util.*;

public class ExceptionHandling {

    public void Divide (double num, double den) {
        
      
        if(den == 0) {

            throw new ArithmeticException("How do I get 'Error: you cannot divide by zero' from the constructor to print when the exception is thrown?");

                
        } else {
            
           
            System.out.println(num/den);
                
        }
        

    }
    public static void main(String args[]) {
        
        
        Scanner input = new Scanner(System.in);
           
        

        System.out.println("Welcome to the division calculator.\nPlease enter the dividend: ");
        double num = input.nextDouble();

        System.out.println("Now please enter the divisor: ");        
        double den = input.nextDouble();

        System.out.println(num + " divided by " + den + " equals: ");

       
        ExceptionHandling a = new ExceptionHandling();        
        a.Divide(num, den);


    }

}


Solution 1:[1]

package modelo;

public class Divicion {

private double numerator;
private double denominator;

public Divicion() {
    numerator = 0;
    denominator = 0;
}

public double getNumerator() {
    return numerator;
}

public void setNumerator(double numerator) {
    this.numerator = numerator;
}

public double getDenominator() {
    return denominator;
}

public void setDenominator(double denominator) {
    this.denominator = denominator;
}

public double operation() throws Exception {
    if (denominator == 0) {
        throw new Exception("You cannot divide by zero");
    }
    return numerator / denominator;
}

}

package calculadora;

import modelo.Divicion; import java.util.Scanner;

public class Calculadora {

public static void main(String[] args) {

    Scanner sc = new Scanner(System.in);
    double cDivide;

    Divicion divide = new Divicion();
    System.out.print("Enter the numerator: ");
    divide.setNumerator(sc.nextDouble());

    System.out.print("Enter the denominator: ");
    divide.setDenominator(sc.nextDouble());

    try {
        cDivide =  divide.operation();
        System.out.println("Result: "+ cDivide);
        
    } catch (Exception e) {
        System.out.println("Error.\n" + e.getMessage());
    }

}

}

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1