Exception

When super class method throws exception then subclass also throws the same Exception, which will compile?

Questions by sunithamule

Showing Answers 1 - 12 of 12 Answers

dev

  • Dec 11th, 2011
 

No, When super class method throws exception then subclass does not throws the same Exception

Yes, we can

Code
  1. package sample;

  2.  

  3.  class Super {

  4.  

  5.        

  6.                 void foo() throws NullPointerException

  7.                 { System.out.println("Super");

  8.                 }

  9.                 }

  10.                 class SupSub extends Super {

  11.                 void foo()throws NullPointerException

  12.                 {

  13.                         System.out.println("SupSub");

  14.                 }

  15.                 }

  16.         public  class super123{

  17.                

  18.                 public static void main( String[] argv ) {

  19.                         SupSub sup=new SupSub();

  20.                         Super spr=new SupSub();

  21.                         spr.foo(); // Output SupSub }

  22.                 }

  23.  

  24. }

  25.  

  Was this answer useful?  Yes

alin20_84

  • Feb 14th, 2012
 

Here foo can skip adding throws NullPointerException because is an unchecked exception
class SupSub extends Super {

void foo()throws NullPointerException

{

System.out.println("SupSub");

}

}

  Was this answer useful?  Yes

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions