The hierarchy of Throwable Exception










class A
{
    void met1() throws Exception
    {
        System.out.println("A");
    }
}
class Throw1 extends A
{
    void met1() throws Exception
    {
        System.out.println("OVERRIDDEN");
    }
    public static void main(String args[])throws Exception
   {
        Throw1 o = new Throw1();
        o.met1();
   }
}

OUTPUT : OVERRIDDEN
If we don’t write throws Exception with main(), there will be a compile error – Unprotected Exception
Same level throws Exception creates no error.

class A
{
    void met1() throws Exception
    {
        System.out.println("A");
    }
}
class Throw2 extends A
{
    void met1()
    {
        System.out.println("OVERRIDDEN");
    }
    public static void main(String args[])
       {
        Throw2 o = new Throw2();
        o.met1();
       }
}

OUTPUT : OVERRIDDEN
No error since over ridden method throwing higher level Exception than the over riding method.

class A
{
    void met1() throws Exception
    {
        System.out.println("A");
    }
}
class Throw3 extends A
{
    void met1() throws RuntimeException
    {
        System.out.println("OVERRIDDEN");
    }
    public static void main(String args[])
       {
        Throw3 o = new Throw3();
        o.met1();
       }
}

OUTPUT : OVERRIDDEN
No error since the method in super class is throwing Exception of the higher level than the over riding method in the sub class from the Throwable hierarchy.

class A
{
    void met1() throws Exception
    {
        System.out.println("A");
    }
}
class Throw4 extends A
{
    void met1() throws Throwable
    {
        System.out.println("OVERRIDDEN");
    }
    public static void main(String args[])
       {
        Throw4 o = new Throw4();
        o.met1();
       }
}

OUTPUT : Compile Error – met1() in Throw4 does not throw Throwable.
Because overriding method is throwing higher level Exception than over method.