javatry-catchnetbeans-8illegalargumentexceptiondividebyzeroexception

Java Fraction Calculator - DivideByZeroMethod through a try/catch


I'm working on a program where the user inputs two different fractions and the computer then runs them through a Fraction method to test if they're equal or not. An additional step in the assignment is to throw an IllegalArgumentException to identify if one of the denominators is 0 and in turn restart the program. The Fraction and testFraction classes are as follows:

Fraction class:

class Fraction {

    private int numerator;
    private int denominator;

    Fraction() {
        numerator = 0;
        denominator = 1;

    }

    Fraction (int num, int den) {
        numerator = num;
        denominator = den;
        if (denominator == 0){
            throw new IllegalArgumentException (
            "Denominator cannot be zero");
        }
    }

    public void setNum(int num){
        numerator = num;
    }

    public void setDen(int den){
        denominator = den;

        }

    public int getNum(){
        return numerator;
    }

    public int getDen(){
        return denominator;
    }

    public String toString() {
        return numerator + "/" + denominator;
        }

    public boolean equals(Fraction other){
    if (numerator * other.getDen() == denominator * other.getNum()){
            return true;
         } 
        else {
            return false;
        }
    }
}

testFraction Class:

import java.util.Scanner;

public class testFraction {

public static void main(String[] args) {

    Scanner keyboard = new Scanner(System.in); 
    int a = 1;
    int b = 1;
    int c = 1;
    int d = 1;

    Fraction f1 = new Fraction(a, b);
    Fraction f2 = new Fraction(c, d);


    System.out.println("Enter the numerator of the first fraction: ");
    f1.setNum(keyboard.nextInt());

    System.out.println("Enter the denominator of the first fraction: ");
    f1.setDen(keyboard.nextInt());

    try{
        int fraction = a/b; 
    }
    catch (IllegalArgumentException e){
        System.out.println(e.getMessage);
        main(args);
    }

    System.out.println("\nEnter the numerator of the second fraction: ");
    f2.setNum(keyboard.nextInt());

    System.out.println("Enter the denominator of the second fraction: ");
    f2.setDen(keyboard.nextInt());

    System.out.println("\nFraction 1: " + f1);
    System.out.println("Fraction 2: " + f2);

    System.out.println("\nAre fractions equal? " + f1.equals(f2));

    System.out.println(("\nWould you like to run the counter with a different integer? \n(1 for YES - 0 for NO): "));
    int answer = keyboard.nextInt();

    if (answer == 1){
        System.out.println("\nLet's Rock 'n Roll! ");
        main(args);
    }

    else if (answer == 0){
        System.out.println("\nLater Dude! ");
        System.exit(0);
    }       
}
}

I feel like I'm incorrectly working in the try/catch. Thanks for your help.


Solution

  • You're throwing an IllegalArgumentException when initializing your Fraction, yet you try to catch it when you do an actual division in testFraction. Your catch will never be reached, since the exception at initialization will stop the code from reaching the incorrect division. You should instead put your try/catch around the code creating Fractions.

    Moreover, you initialize your Fractions with mock values : 1. This isn't really useful, and pose a problem : your constructor will never throw its IllegalArgumentException, since it is called with correct values. You should raise the IllegalArgumentException in your setDenominator instead, and call it from your constructor.