Why am I getting this error of incompatible types

Error in code!

import java.util.Scanner;

import java.util.InputMismatchException;

public class exceptionTest {
    public static void main(String[] args) {
        Scanner scan = new Scanner(System.in);

        int n1, n2;

        double r;

        boolean answer = false;

        while (!answer) {
            try {
                System.out.println("What is the value of n1?");

                n1 = scan.nextInt();

                System.out.println("What is the value of n2?");

                n2 = scan.nextInt();

                if (n2 == 0)

                    throw new DividebyZeroException();


                r = (double) n1 / n2;

                System.out.println("Answer: " + n1 + "/" + n2 + "=" + r);

                answer = true;

            } catch (DividebyZeroException dvz) {
                scan.nextLine();

                System.out.println("you have attempted to divide by zero! Please try again!");
            } catch (InputMismatchException text) {
                scan.nextLine();

                System.out.println("You have entered a character and not an integer!");
            }


        }

    }

}



public class DividebyZeroException extends Exception
{
    public DividebyZeroException()
    {
        super("Divison by Zero!");
    }

    public DividebyZeroException(String banner)
    {
        super(banner);
    }
}
Matt VilkAsked:
Who is Participating?
 
Jeffrey Dake Senior Director of TechnologyCommented:
Have you double checked that it is extending java.lang.Exception.  Try putting the in explicitly to make sure the compiler is picking up the right class.

public class DivideByZeroException extends java.lang.Exception
{

   public DivideByZeroException()
    {
        super("Divison by Zero!");
    }

    public DivideByZeroException(String banner)
    {
        super(banner);
    }
}

Open in new window

0
 
Jeffrey Dake Senior Director of TechnologyCommented:
Can you paste in the exact error message you are getting?   Are you getting this when you compile or when you run the program?
0
 
Matt VilkAuthor Commented:
This is the error(s).
Capture.PNG
0
Cloud Class® Course: Ruby Fundamentals

This course will introduce you to Ruby, as well as teach you about classes, methods, variables, data structures, loops, enumerable methods, and finishing touches.

 
Jeffrey Dake Senior Director of TechnologyCommented:
Everything looks fine when I compile that exception class.  Do you have the exception class in its own file, and what are you using to compile?  Are you using an editor of some sort, or are you compiling by hand?
0
 
Jeffrey Dake Senior Director of TechnologyCommented:
I also assume you are extending java.lang.Exception and not some other Exception class from somewhere.  Is that correct?
0
 
Matt VilkAuthor Commented:
i have an exception class in its own file, intelliJ to compile.
0
 
Matt VilkAuthor Commented:
Thanks that worked!

Thanks for the help!!!
0
 
krakatoaCommented:
To confirm what Jeff is saying, your code above compiles and runs as it should. There must be a bad reference somewhere.
0
 
CEHJCommented:
Please note, that although your code works, it doesn't really make sense to throw exceptions and catch them in the same method. Exceptions are not only about dealing with errors - they are about flow control and alternative code paths. You might just as well deal with the situation, and much more easily, by checking and then changing the flow. IOW an exception isn't needed here
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.