Page 2 of 2 FirstFirst 12
Results 21 to 32 of 32
  1. #21
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: help with exceptions

    It does, but the test still fails.
    Still on the same line - method MyStorage.store() threw unexpected CannotStoreException() - error during storing data in remote server. It relates to the situation, where Connection.sendData() throws exception for the first time and succeds on the second time.

  2. #22
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,925
    Rep Power
    19

    Default Re: help with exceptions

    Stick some debugging lines in there (println() calls) so you can see where in the flow it is going, because as it stands that code looks OK to me.
    You'll want println() calls throughout that method, including the values of various variables (like retry and maxTries).
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  3. #23
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: help with exceptions

    I tried to print retries and maxAttempts as last two commands, but they were strangely always 1.
    Anyways thanks you so much for your help.
    Just if you see anything in these two pieces of code, let me know.
    Here is my code:

    Java Code:
    public class MyStorage implements Storage {
        private Connector connector;    
        private int maxAttempts;
        private Connection conn;
        private int retries = 1;
        
        public MyStorage(Connector connector, int maxAttempts) /*throws NullPointerException, IllegalArgumentException*/ {
            
            if(connector == null) throw new NullPointerException("connector");        
            if(maxAttempts < 1) throw new IllegalArgumentException("maxAttempts");
            
            this.connector = connector;
            this.maxAttempts = maxAttempts;
        }
    
        public void store(String host, Object data) throws DbException {
            try {
                Connection conn = connector.getConnection(host);
                conn.sendData(data);
            } catch (UnknownHostException e) {
                throw new DbUnreachableException("", e);
            }
              catch (NoRouteToHostException e) {
                throw new DbUnreachableException("", e);
            }
              catch (IOException e) {
                throw new CannotStoreException("", e);
            }   
                    
            while(retries < maxAttempts) {
                try {
                    conn.sendData(data);
                    break;
                } catch (IOException e) {
                    retries++;             
                    if(retries == maxAttempts) throw new CannotStoreException("", e);
                }
            }
            
            System.out.println(retries);
            System.out.println(maxAttempts);
        }  
    }
    Here is the test, where it fails (lines 8-12):

    Java Code:
            // test sending data with error and retry is ok
            connector = new TestConnector();
            st = new MyStorage(connector,2);
    	connector.dataException = new IOException();
    	connector.dataExceptionCounter = 1;
    	try {
                st.store("address1", circle);
    	} catch (Exception ex) {
    	    fail("Methoda MyStorage.store() threw unexpected exception " 
    		    + ex + " (it relates to the situation, where method " +
    		    "Connection.sendData() throws exception for the first time and succeeds " +
    		    "for the second time)");
    	}
    	assertEquals("Pri posilani gr. objektu na vzdaleny server nebyl objekt predan konektoru",
    		circle,connector.sentData);
    	assertEquals("Spojeni nebylo navazano se spravnou adresou","" +
    		"address1",connector.host);
    	assertEquals("Metoda Connector.getConnection() byla zbytecne volana vicekrat",
    		1,connector.getConnectionCounter);
    	assertEquals("Metoda Connection.sendData() byla zbytecne volana vicekrat",
    		2,connector.sendDataCounter);

  4. #24
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,925
    Rep Power
    19

    Default Re: help with exceptions

    Declare retries in the method itself, not as an attribute of the class, and initialise it to zero.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  5. #25
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: help with exceptions

    Same result. Well, it seems I will have to be hoping for a miracle here. :-)
    But you have been a great help. Again, thank you so much.

  6. #26
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,925
    Rep Power
    19

    Default Re: help with exceptions

    It could the test case is wrong, of course...
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  7. #27
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: help with exceptions

    Itīs not, I specifically asked. :-)

  8. #28
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,925
    Rep Power
    19

    Default Re: help with exceptions

    Ah.
    You're still calling sendData in your first try/catch block.
    Remove that one, as the following while loop is now supposed to be handling all calls for that.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #29
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: help with exceptions

    But that is for the first time test of sending data. I canīt remove this line, because there would be no object to pass to the connector and the first test would fail.

  10. #30
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,925
    Rep Power
    19

    Default Re: help with exceptions

    That line needs to be removed for your code to follow the requirements.
    At the moment you are always going to sendData twice at least.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  11. #31
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    11,925
    Rep Power
    19

    Default Re: help with exceptions

    Oh yes, I see the problem:
    Java Code:
    Connection conn = connector.getConnection(host);
    That is declaring a new, local, 'conn' variable, which is not the one help by your MyStorage class as an attribute.
    If you remove the 'Connection' you will be assigning the result of getConnection() to your proper attribute.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  12. #32
    dawnMist is offline Member
    Join Date
    Oct 2012
    Posts
    43
    Rep Power
    0

    Default Re: help with exceptions

    I really appreciate your help with this. But itīs not that either. Still it does not pass the object when I dont put the sendData method in the first try block and when I put it in there, it throws the Exception where it should succeed (for the second time).

    EDIT: OMG - you were right, I removed the sendData from the first try block AND I had to put my number of retries (iterator) = 0, I had "retries = 1;".
    Now it works perfectly. Thank you so much for your help, you are a saint.
    Last edited by dawnMist; 11-15-2012 at 10:50 AM.

Page 2 of 2 FirstFirst 12

Similar Threads

  1. Exceptions
    By Maya in forum New To Java
    Replies: 2
    Last Post: 05-24-2011, 06:30 AM
  2. Exceptions?
    By linc186 in forum New To Java
    Replies: 3
    Last Post: 03-07-2011, 08:03 AM
  3. Exceptions
    By Nerijus in forum New To Java
    Replies: 8
    Last Post: 05-18-2010, 01:44 PM
  4. Exceptions & More
    By besweeet in forum New To Java
    Replies: 12
    Last Post: 04-29-2010, 09:06 PM
  5. Exceptions
    By hedonist in forum New To Java
    Replies: 10
    Last Post: 09-08-2009, 08:38 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
  •