creating an exception

i have a data file filled with records and i need to create an exception for when a record isn't found.  i figure that it won't be found if the end of file has been reached before finding it.  so in my exception class, would i just extend the ioexception class and then in my method code to retrieve a record, catch an eofexception and throw my exception in its catch block?
abstractionzAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

CEHJCommented:
Just create a subclass of Exception.

if (recordNotFound) {
    throw new RecordNotFoundException("Not found!");
}
0
abstractionzAuthor Commented:
you have the conditional if(recordNotFound).  how do i know when the end of file has been reached so i can throw my exception, other than catching an end of file exception?
0
CEHJCommented:
If the loop in which you are reading the file has finished and it has not flagged recordNotFound = false during its execution, then throw one.
0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

abstractionzAuthor Commented:
theres no loop though.  it reads the specified record number.
0
CEHJCommented:
In that case

if (recordRead.equals(recordToFind) == false) {
    throw ...
}
0
abstractionzAuthor Commented:
its kind of hard to check that condition.  there's only a record number supplied which helps jump to the correct byte position to retrieve the record.
0
abstractionzAuthor Commented:
thats why i was thinking of throwing my exception when i catch an eofexception
0
CEHJCommented:
I don't understand. If you have an offset at which to read a record there shouldn't be any possibility of reaching EOF if the code is correct.
0
abstractionzAuthor Commented:
the number of records is unknown but lets say there are 25.  if the record number supplied is 30, then there will be an eofexception, because to get to that record the number of bytes i skip is the record number * the number of bytes in a record
0
abstractionzAuthor Commented:
so it's for record numbers that don't exist
0
CEHJCommented:
>>the number of records is unknown

Presumably, you know

a. the size of the file
b. the size of a record

The number of records = filesize/recordsize
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
abstractionzAuthor Commented:
i never thought of that.  nice.  how do i get the filesize?
0
objectsCommented:
try
{
   // look for record
}
catch (EOFException ex)
{
   throw new RecordNotFoundException();
}
0
abstractionzAuthor Commented:
yea that's the code i originally thought of writing
0
CEHJCommented:
It's not particularly good to deliberately cause excepetions
0
objectsCommented:
> It's not particularly good to deliberately cause excepetions

It may not be deliberately caused.
0
abstractionzAuthor Commented:
is there a function to get the total number of bytes in a file?
0
CEHJCommented:
Exceptions, by definition, are concerned with unforeseen conditions in a program. The potential absence of a record is not such a condition. It's a perfectly normal outcome of a normal run of a program and should not cause an exception.
0
CEHJCommented:
long fileLength = file.length();
0
abstractionzAuthor Commented:
thanks. i'm going to go with your suggestion cehj.  it will help me with my exception and also in the future when i need to get the record number of a record i have just added somewhere in the file.
0
CEHJCommented:
8-)
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Java

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.