doubt in Exceptions

Posted by Ajay Singh on Stack Overflow See other posts from Stack Overflow or by Ajay Singh
Published on 2011-03-03T07:17:27Z Indexed on 2011/03/03 7:25 UTC
Read the original article Hit count: 182

Filed under:
class MyException extends Exception {
    MyException() {}
    MyException(String msg) { super(msg);}
}
public class NewException {

    static void f() throws MyException {
        System.out.println("throwing exception from f()");
        throw new ClassCastException();
    }
    static void g() throws MyException {
        System.out.println("throwing exception from g()");
        throw new MyException("parametrized ");
    }
    public static void main(String ...strings ) {
        try { 
            f();
        }
        catch(MyException e) {
            e.printStackTrace(System.out);
        }
        try {
            g();
        }
        catch(MyException e) {
            e.printStackTrace(System.out);
        }
    }
}

In the function f() iam specifying that "MyException " exception will be thrown and actually iam throwing some other exception which has no relation with MyException but still the compiler does not report any complain.Why is it so??

© Stack Overflow or respective owner

Related posts about java