Win a copy of The Little Book of Impediments (e-book only) this week in the Agile and Other Processes forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

URLyBird Throwing exceptions

 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
For UB, I need RecordNotFoundException and DuplicateKeyException.
The read(int recNo) method, it throws RecordNotFoundException under conditions where 1) recNo not found or 2) delete flag is 1.
So in my read() method, if I have:


And in my main testing program I have:


Suppose there are 32 records in data file and I run read(33) I should get the RecordNotFoundException. But I'm always getting the one in main program rather than the one I throw inside the read() method (ie with recNo). How to I get the exception with the record number? I don't think I can catch it inside the read() method because that defeats the purpose of having a throws clause.
[ December 27, 2008: Message edited by: K. Tsang ]
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I hope you are pointing the file pointer directly to the record number and its location using RAF seek method in your read method. You can catch <b>IOException</b> in read method and check whether it's an <b> instanceof EOFException </b>. and re-throw the <b>RecordNotFoundException</b>

for example
[code]
if(ie instanceof EOFException)
throw new RecordNotFoundException(res.getString("eof_reached") + " " + recNo + " " +
res.getString("recno_not_found"), ie.getCause());

</code>

in your client, you can just catch RecordNotFoundException and display the Messagebox using <b> getMessage </b> method.

For example

<code>
try {

// invoke read(recNo) here
}
} catch(RecordNotFoundException rnfe) {
System.err.println(rnfe.getMessage());
or
// JOptionPane msg box
}

</code>

sat
 
satishkumar janakiraman
Ranch Hand
Posts: 334
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi

For delete option, you have to validate like this



in my previous post i gave you the code snippet for throwing exception for invalid record no.
I hope now you get an idea how to throw & catch exception

sat
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks satishkumar I'm able to use the custom messages through the ioe.getMessage() method.
 
Jeffry Kristianto Yanuar
Ranch Hand
Posts: 759
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by K. Tsang:
[QB]For UB, I need RecordNotFoundException and DuplicateKeyException.
The read(int recNo) method, it throws RecordNotFoundException under conditions where 1) recNo not found or 2) delete flag is 1.
So in my read() method, if I have:


And in my main testing program I have:



What if the recNo is negative ? Did you already validate in the method?


Jeffry Kristianto Yanuar (Java Instructor) SCJP 5.0, SCJA, SCJD (UrlyBird 1.3.2)
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And to complement what my good buddy Jeffry said, what if recNo <= totalRecords, but its flag == 1? If you check only if (recNo > totalRecords), then you won't be contemplating your second rule.
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ah good point, recNo can be negative, 0, <= totalRecords, or > totalRecords. If > throw RecordNotFoundException simple. If neg or 0 throw IOException simple. If <= read data.

However, if the record has delete=1, I can say that record is deleted / unaccessible or I can jump to the next non-deleted record and return the row's data. Currently I'm using the first approach, which should allow me to make my create() method reuse deleted rows.
 
Roberto Perillo
Bartender
Posts: 2271
3
Eclipse IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If neg or 0 throw IOException simple.


How would that be, partner? You can't throw any exception other than RecordNotFoundException.
 
K. Tsang
Bartender
Posts: 3585
16
Android Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Roberto Perillo:


How would that be, partner? You can't throw any exception other than RecordNotFoundException.


Yes you are right. If I don't explicitly check if recNo<=0, IOException will catch it when I execute the RandomAccessFile's seek() method. So I have added a checking at the beginning to check recNo<=0 and throw RNFE.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic