-3

here i am creating my custom exception and want to throw exception on divide method for wrong input till then it was fine i am able to throw my custom exception but problem in catching it code as follows

class A extends Exception {
    A(String s) {
        super(s);
    }
}

class Emp {
    int a;
    int b;

    void divide(int a, int b) throws A {
        if (b == 0) {
            throw new A("super exception is there");
        } else
            System.out.println(a / b);

    }

    public static void main(String args[]) {
        Emp m = new Emp();
        try {
            m.divide(10, 0);
        } catch (A e) {
            System.out.println(e);
        }

    }
}

it giving me error main method not found me A class
unable to figure out why this is happening

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
Sky1703
  • 13
  • 3
  • At least format your code so it's marginally readable. You also need to include at least some of the error dialogue. – user1231232141214124 Aug 24 '13 at 19:39
  • 3
    This question appears to be off-topic because it asks to fix a working code. – Denys Séguret Aug 24 '13 at 19:43
  • A stylistic suggestion: Your custom `Exception` should be named something like `AException` or `CustomDivideByZeroException`, so that other developers (and yourself later on) can easily tell that this is an `Exception`. – SimonT Aug 24 '13 at 19:43

3 Answers3

1

Since, your main() method is defined in the Emp class rename your .java file to Emp.java. It's probably A.java right now because that's why Java is looking for the main() method in class A.

Ravi K Thapliyal
  • 51,095
  • 9
  • 76
  • 89
0

i have tried your code and it is working fine. returning the following result:

A: super exception is there

What is the name of your java file. I have named it as Emp.java as Emp class is containing main method.

Ahsan Shah
  • 3,931
  • 1
  • 34
  • 48
0

That is because your main-Method is in your Emp class not in the A class.

Name your sourcefile "Emp.java" and compile it with "javac Emp.java" if you use the command line and than run it with "java Emp".

user2489650
  • 29
  • 1
  • 7