Search code examples
javaexceptioncustom-exceptions

created my custom exception unable to catch it properly


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


Solution

  • 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.