• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Exception - Abhilash Mock

 
Ranch Hand
Posts: 143
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Read the following code below.
public interface AQuestion
{
public abstract void someMethod() throws Exception;
}
A Class implementing this interface should

1.Necessarily be an abstract class.
2.Should have the method public abstract void someMethod();
3.Should have the method public void someMethod() which has to throw an exception which is a subclass of java.lang.Exception.
4.Should have the method public void someMethod() which need not throw an Exception.
answer given is 4, How about 3??
-Arun
 
Bartender
Posts: 2205
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
3 is not correct because the method is not required to throw any exceptions. IFF it throws an exception, then the type of the exception must be the same as, or a subclass of, the type declared in the interface.
 
Ranch Hand
Posts: 50
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Arun Pai:
Read the following code below.
public interface AQuestion
{
public abstract void someMethod() throws Exception;
}
A Class implementing this interface should

1.Necessarily be an abstract class.
2.Should have the method public abstract void someMethod();
3.Should have the method public void someMethod() which has to throw an exception which is a subclass of java.lang.Exception.
4.Should have the method public void someMethod() which need not throw an Exception.
answer given is 4, How about 3??
-Arun


First off, we should know subclasses' overriding methods could only throw the same exception or narrow down parent's exception.
For this example, in my opinion, 3 is not right, because it states

public void someMethod() which has tothrow an exception which is a subclass of java.lang.Exception.


public void someMethod() throws IOException
public void someMethod() throws Exception
public void someMethod()
are all right.
 
Ranch Hand
Posts: 317
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello,
Q3 is wrong, because it says that the method "has to throw an exception which is a subclass of java.lang.Exception."...notice the word "..has to".....implemented method is not required to throw any exceptions...
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic