Prathyusha Lakkadi
Prathyusha Lakkadi

Reputation: 9

why my java code fails when i'm trying to typecast inside try block?

I've written the code this way where the method division returns double value after dividing two integers.It works fine if i do not include try catch block.But when I enclose the integer division with type casting inside try block as shown below, it leads to compilation problem... please solve my problem.

import java.util.Scanner;

class Division {

    /**
     * @param args
     */
    public static void main(String[] args) {
        Scanner sc=new Scanner(System.in);
        Division div=new Division();
        System.out.println("Enter the two numbers to perform division operation:");
        int x=sc.nextInt();
        int y=sc.nextInt();
        div.division(x,y);
    }

    public double division(int x,int y){
        try{
            double z=(double)(x/y);
            return z;
            }
        catch(ArithmeticException ae){
            ae.printStackTrace();
        }
    }

}

Upvotes: 0

Views: 94

Answers (4)

David Riott
David Riott

Reputation: 15

The issue is that if your try block fails, there's no return statement found (since the return you provided was local only to the try block).

So as others have pointed out, you could either return something (e.g. -1) outside both the try and the catch blocks (but still inside the method), or you could have a return statement in the catch block as well, so even if the try throws an exception, your method still returns a double.

Upvotes: 1

Shrinivas Shukla
Shrinivas Shukla

Reputation: 4463

Your method must have a return statement.

Your current code don't have return statement if it enters the catch block.

Try this code.

import java.util.Scanner;

public class Division {

    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Division div = new Division();
        System.out.println("Enter the two numbers to perform division operation:");
        int x = sc.nextInt();
        int y = sc.nextInt();
        div.division(x, y);
    }

    public double division(int x, int y) {
        double z = 0;
        try {
            z = (double) (x / y);
        } catch (ArithmeticException ae) {
            ae.printStackTrace();
        }
        return z;
    }

}

Live Demo here.

Also I guess there is data loss in the way you are type-casting.
If 17/3 = 5.6666666 is what you want, then your code is WRONG because x and y are int. The output you will get with the current code is 17/3=5

Instead of z = (double) (x / y);, you need z = (double) x / (double) y;

Upvotes: 1

Sanshayan
Sanshayan

Reputation: 1076

Your method missing the return statement. Try this one

 public double division(int x,int y){
        double z=0.0;
        try{
             z=(double)(x/y);

        }
        catch(ArithmeticException ae){
            ae.printStackTrace();
        }
        return z;
    }

Upvotes: 1

Weston Jones
Weston Jones

Reputation: 161

You are missing a return in your division function. By catching the exception you are saying that you are going to do something to handle it in the case that there is an issue and execution will continue after.

It would probably be best to just throw the exception here because if you divide by zero there will be nothing to return. Optionally you could return something nonsensical like -1

public double division(int x,int y){
    try{
        double z=(double)(x/y);
        return z;
        }
    catch(ArithmeticException ae){
        ae.printStackTrace();
    }
    return -1;
}

An even better solution would be to throw an exception if the divisor is 0 and then handle it wherever it is used

public double division(int x, int y) throws ArithmeticException {
        if (y == 0)
            throw new ArithmeticException("Cannot divide by 0");
        double z = (double) (x / y);
        return z;
    }

Upvotes: 1

Related Questions