Results 1 to 14 of 14
  1. #1
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default Overloaded methods throwing exception

    Quoting the SCJP book by Kathy Sierra and Bert Bates:
    Overloaded methods CAN declare new or broader checked exceptions.
    We know that FileNotFoundException is a subclass of IOException and according to the above statement an overloaded method cannot throw a narrower exception. But the below code does not throw a compiler error. How?

    Animal.java
    Java Code:
    package pack1;
    
    import java.io.FileNotFoundException;
    import java.io.IOException;
    
    public class Animal{
    		public void drink(int s) throws IOException
    		{
    			
    		}
    }
    Horse.java
    Java Code:
    package pack2;
    import java.io.FileNotFoundException;
    
    import pack1.Animal;
    public class Horse extends Animal {
    	public void drink() throws FileNotFoundException
    	{
    		
    	}
    }

  2. #2
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Overloaded methods throwing exception

    No, it cannot throw a wider exception.

    So swap the FileNotFound with the IOException.

    It makes no sense for it not to be able to throw a child exception as (as you say) an FNF exception is-an IO exception, so would not be breaking the contract.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #3
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default Re: Overloaded methods throwing exception

    No, it cannot throw a wider exception.

    I am talking about overloading not overridding. Eventhough I have used a super class and sub class I am overloading the method in the subclass.

  4. #4
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Overloaded methods throwing exception

    No you're not.
    You are overiding it.

    You may not think you are, but you are.

    If you were overloading it it would have different parameters, which it doesn't.

    ETA: Oh, flip.
    Missed the "int s".
    Sorry.

    OK.
    So why do you think this shouldn't compile?
    Because you'll need to provide more than that one sentence from Bates et al.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #5
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default Re: Overloaded methods throwing exception

    So why do you think this shouldn't compile?

    That I dont know. Dont ask counter-questions. What is the meaning of
    Overloaded methods CAN declare new or broader checked exceptions.?

    What about narrower checked exceptions?

  6. #6
    jashburn is offline Senior Member
    Join Date
    Feb 2014
    Posts
    219
    Rep Power
    1

    Default Re: Overloaded methods throwing exception

    Overloaded methods CAN declare new or broader checked exceptions.
    That is correct, and I quote from Chapter*8.*Classes,
    There is no required relationship between the return types or between the throws clauses of two methods with the same name, unless their signatures are override-equivalent.
    In other words, the overloading method can throw anything it likes, hence "new or broader checked exceptions".

    This is not to be confused with method overriding.

    In the example in the original post, the Animal class has
    Java Code:
    public void drink(int s) throws IOException
    whereas the Horse class has
    Java Code:
    public void drink() throws FileNotFoundException
    i.e., the parameters differ, and so overloading this is.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Overloaded methods throwing exception

    "Overloaded methods CAN declare new or broader checked exceptions.?"

    You'll have to give us more to go on.
    That one sentence is clearly removed from the context around it.

    And:
    "Don't ask counter-questions."

    Um, it's not my fault your question is (as it stands) too vague.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Overloaded methods throwing exception

    Oh right.
    So it's stating the bleeding obvious.
    No wonder it didn't make any sense to me.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Overloaded methods throwing exception

    Overloading is a compiler thingie, i.e. it figures out what method to call by inspecting the types in their argument list; only to humans (see Tolls ;-) overloaded method may seem identical but they're completely unrelated; unrelated method can throw whatever Exception they want. That single quoted phrase needs more context to make sense.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    4,033
    Rep Power
    6

    Default Re: Overloaded methods throwing exception

    Overloaded methods CAN declare new or broader checked exceptions.
    Even within context (whatever that would be) this sentence could be misleading. A broader checked exception is a new exception so it was poorly worded, IMHO. It's like saying "pick a number. Now pick any other number or one greater than the first number".

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Overloaded methods throwing exception

    Quote Originally Posted by jim829 View Post
    Even within context (whatever that would be) this sentence could be misleading. A broader checked exception is a new exception so it was poorly worded, IMHO. It's like saying "pick a number. Now pick any other number or one greater than the first number".
    It depends on what the context is; if the previous sentence were 'inherited methods can only throw identical Exceptions or subclasses thereof.' it would've made more sense ...

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  12. #12
    Join Date
    Aug 2013
    Posts
    55
    Rep Power
    0

    Default Re: Overloaded methods throwing exception

    Overloaded methods CAN declare new or broader checked exceptions.

    Maybe the author wanted to show in contrast to overridding where we CANNOT throw new or broader checked exception.

    A broader checked exception is a new exception so it was poorly worded, IMHO
    I believe that new exception and broader checked exception are referring to two different things. They are not the same IMO.

  13. #13
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    12,224
    Rep Power
    20

    Default Re: Overloaded methods throwing exception

    Quote Originally Posted by JosAH View Post
    Overloading is a compiler thingie, i.e. it figures out what method to call by inspecting the types in their argument list; only to humans (see Tolls ;-) overloaded method may seem identical but they're completely unrelated; unrelated method can throw whatever Exception they want. That single quoted phrase needs more context to make sense.

    kind regards,

    Jos
    **thrrrrrp**
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  14. #14
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,783
    Blog Entries
    7
    Rep Power
    21

    Default Re: Overloaded methods throwing exception

    Quote Originally Posted by Tolls View Post
    **thrrrrrp**
    That does not compute; that does not compute.

    kindest regards,

    Mork from Ork (nanu, nanu)
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Throwing A Character Exception
    By Mnelson in forum New To Java
    Replies: 4
    Last Post: 07-19-2012, 11:07 PM
  2. Program not throwing exception
    By aedara in forum New To Java
    Replies: 3
    Last Post: 06-28-2011, 06:55 AM
  3. can static methods be overloaded?
    By katturv in forum New To Java
    Replies: 2
    Last Post: 10-03-2010, 07:51 PM
  4. Exception throwing
    By andy16 in forum New To Java
    Replies: 9
    Last Post: 06-07-2010, 06:57 PM
  5. throwing Exception
    By bugger in forum New To Java
    Replies: 3
    Last Post: 11-09-2007, 10:35 PM

Posting Permissions

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