Results 21 to 32 of 32
Thread: help with exceptions
- 11-14-2012, 02:50 PM #21
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
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.
- 11-14-2012, 03:30 PM #22
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 11-14-2012, 03:54 PM #23
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
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:
Here is the test, where it fails (lines 8-12):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); } }
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);
- 11-14-2012, 04:26 PM #24
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 11-14-2012, 05:04 PM #25
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
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.
- 11-14-2012, 05:31 PM #26
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: help with exceptions
It could the test case is wrong, of course...
Please do not ask for code as refusal often offends.
- 11-14-2012, 05:34 PM #27
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
Re: help with exceptions
It´s not, I specifically asked. :-)
- 11-14-2012, 05:59 PM #28
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 11-14-2012, 06:08 PM #29
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
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.
- 11-15-2012, 09:32 AM #30
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
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.
- 11-15-2012, 09:35 AM #31
Moderator
- Join Date
- Apr 2009
- Posts
- 10,481
- Rep Power
- 16
Re: help with exceptions
Oh yes, I see the problem:
That is declaring a new, local, 'conn' variable, which is not the one help by your MyStorage class as an attribute.Java Code:Connection conn = connector.getConnection(host);
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.
- 11-15-2012, 10:09 AM #32
Member
- Join Date
- Oct 2012
- Posts
- 43
- Rep Power
- 0
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.
Similar Threads
-
Exceptions
By Maya in forum New To JavaReplies: 2Last Post: 05-24-2011, 06:30 AM -
Exceptions?
By linc186 in forum New To JavaReplies: 3Last Post: 03-07-2011, 08:03 AM -
Exceptions
By Nerijus in forum New To JavaReplies: 8Last Post: 05-18-2010, 01:44 PM -
Exceptions & More
By besweeet in forum New To JavaReplies: 12Last Post: 04-29-2010, 09:06 PM -
Exceptions
By hedonist in forum New To JavaReplies: 10Last Post: 09-08-2009, 08:38 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks