Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: interface - public void method() throws SomeException;

  1. #1
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default interface - public void method() throws SomeException;

    I need to write the implementation for the following interface. I noticed that "throws SomeException" are marked in the method declaration header. Does it mean I must specify the particular precondition and construct that particular exception object for a throw statement in the method?

    public interface MinHeap<E> {

    public void insert(E item) throws HeapOverflowException;
    public E findMin() throws NoSuchElementException;
    public E deleteMin() throws NoSuchElementException;
    }


  2. #2
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default public void method() throws SomeException;

    I need to write the implementation for the following interface. I noticed that "throws SomeException" are marked in the method declaration header. Does it mean I must specify the particular precondition and construct that particular exception object for a throw statement in the method?

    public interface MinHeap<E>
    {
    public void insert(E item) throws HeapOverflowException;
    public E findMin() throws NoSuchElementException;
    public E deleteMin() throws NoSuchElementException;
    }

  3. #3
    Member angstrem's Avatar
    Join Date
    Mar 2013
    Location
    Ukraine
    Posts
    200
    My Mood
    Happy
    Thanks
    9
    Thanked 31 Times in 29 Posts

    Default Re: public void method() throws SomeException;

    "throws" means just a possibility. It can happen, and it can never happen. By the way, when I want to test something quickly, I often do it in the main method with following signature:
    public static void main(String[] args) throws Exception
    And it works even if the body does not throw any exception.

  4. The Following User Says Thank You to angstrem For This Useful Post:

    JavaOwl (June 6th, 2013)

  5. #4
    Super Moderator jps's Avatar
    Join Date
    Jul 2012
    Posts
    2,642
    My Mood
    Daring
    Thanks
    90
    Thanked 263 Times in 232 Posts

    Default Re: interface - public void method() throws SomeException;

    Please do not double post your questions

    Threads merged

  6. The Following User Says Thank You to jps For This Useful Post:

    JavaOwl (June 6th, 2013)

  7. #5
    Junior Member
    Join Date
    Jun 2013
    Posts
    3
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Default Re: public void method() throws SomeException;

    I still need to include the codes in the body to handle the exception either
    if (precondition) { throw new SomeException();}
    or
    try/catch block
    right?

    if that's the case, why do I need to put "throws Exception" in the method header?

    --- Update ---

    got it. thanks.

Similar Threads

  1. public void run () error message
    By Niquinho in forum What's Wrong With My Code?
    Replies: 12
    Last Post: December 23rd, 2012, 02:22 AM
  2. public static void main(NewMember Mr_Bukkake){ }
    By Mr. Bukkake in forum Member Introductions
    Replies: 2
    Last Post: October 13th, 2011, 07:29 AM
  3. Calling a void method into a static void main within same class
    By sketch_flygirl in forum Object Oriented Programming
    Replies: 3
    Last Post: November 15th, 2009, 05:24 PM
  4. [SOLVED] Is "Public void closeFile()" a problem in the program for AS-Level computing project
    By muffin in forum What's Wrong With My Code?
    Replies: 2
    Last Post: August 5th, 2009, 09:12 PM

Tags for this Thread