Results 1 to 10 of 10
  1. #1
    dnzzn is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default Exception Handling Related

    In Java HTPe7 by Deitel, Common Programming Error 13.5 says that:

    "If a subclass method overrides a superclass method, it is an error for the superclass method to list more exceptions in its throws clause than the overridden superclass method does. However, a subclass's throws clause can contain a subset of superclass's throws list."

    Well this kind of makes sense and seems self-explanatory. However, there are some methods overridden in subclasses that i do need to throw an exception (i.e. ArithmeticException) whereas in superclass method it is not needed. I mean what kind of an error is this anyways?

    Thanks in advance

  2. #2
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  3. #3
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Quote Originally Posted by dnzzn View Post
    In Java HTPe7 by Deitel, Common Programming Error 13.5 says that:

    "If a subclass method overrides a superclass method, it is an error for the superclass method to list more exceptions in its throws clause than the overridden superclass method does. However, a subclass's throws clause can contain a subset of superclass's throws list."

    Well this kind of makes sense and seems self-explanatory. However, there are some methods overridden in subclasses that i do need to throw an exception (i.e. ArithmeticException) whereas in superclass method it is not needed. I mean what kind of an error is this anyways?

    Thanks in advance
    ArithmeticException is a runtime exception. The rules apply to checked exceptions.

  4. #4
    dnzzn is offline Member
    Join Date
    Sep 2009
    Posts
    3
    Rep Power
    0

    Default

    No, I gave ArithmeticException just as an example. What I really meant to show was that a subclass method overriding a superclass method such that the superclass method does not need a throws clause, but the subclass method actually needs it. And I actually tried this and did not give any errors at all.

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    7

    Default

    Post your code so we can understand what you mean.

  6. #6
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    Quote Originally Posted by r035198x View Post
    Post your code so we can understand what you mean.
    Yes it is. Without knowing what there is difficult to comment on it.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,742
    Rep Power
    19

    Default

    Simple bit of code.

    Parent class, with a single method that throws no exceptions.

    Java Code:
    public class Parent {
    
    	public void doSomething()
    	{
    		System.out.println("Do something");
    	}
    }
    Child subclass of parent that throws and exception in the overridden method.

    Java Code:
    public class Child extends Parent {
    
    	public void doSomething() throws ParentException
    	{
    		System.out.println("Do something");
    	}
    
    }
    (the exception simply extends Exception).
    This is a compilation error...so I don't quite see how you are managing to do this without an exception, if I've read your post correctly.

  8. #8
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

    Default

    I'm not exactly clear what you mean here. My confusion here is without throws the exception in overridden method in parent class how can you through an exception from the subclass. Basically compiler is not able to find the appropriate method in two classes.

  9. #9
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    19

  10. #10
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,742
    Rep Power
    19

    Default

    Quote Originally Posted by Eranga View Post
    I'm not exactly clear what you mean here. My confusion here is without throws the exception in overridden method in parent class how can you through an exception from the subclass. Basically compiler is not able to find the appropriate method in two classes.
    Was that in reply to my example?
    Well, dnzzn said:

    Quote Originally Posted by dnzzn View Post
    What I really meant to show was that a subclass method overriding a superclass method such that the superclass method does not need a throws clause, but the subclass method actually needs it. And I actually tried this and did not give any errors at all.
    Which implied to me that they had written a subclass overiding a method, but also threw an exception that the parent didn't throw. My example showed that that's not actually possible. It's a compilation error.

Similar Threads

  1. Exception handling and logging
    By jurka in forum New To Java
    Replies: 8
    Last Post: 09-03-2008, 07:07 PM
  2. Exception Handling...
    By focus_nitin in forum New To Java
    Replies: 1
    Last Post: 02-16-2008, 03:13 AM
  3. JDBC - Exception handling
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-05-2007, 04:00 PM
  4. Jstl Exception Handling
    By vamsidharpoosarla in forum JavaServer Pages (JSP) and JSTL
    Replies: 2
    Last Post: 07-18-2007, 06:17 AM
  5. JSTL Exception Handling
    By chaatf in forum JavaServer Pages (JSP) and JSTL
    Replies: 1
    Last Post: 07-18-2007, 02:24 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •