Solved

creating an exception

Posted on 2003-11-28
21
378 Views
Last Modified: 2010-03-31
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?
0
Comment
Question by:abstractionz
  • 10
  • 9
  • 2
21 Comments
 
LVL 86

Expert Comment

by:CEHJ
ID: 9839448
Just create a subclass of Exception.

if (recordNotFound) {
    throw new RecordNotFoundException("Not found!");
}
0
 

Author Comment

by:abstractionz
ID: 9839475
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9839495
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
Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

 

Author Comment

by:abstractionz
ID: 9839502
theres no loop though.  it reads the specified record number.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9839525
In that case

if (recordRead.equals(recordToFind) == false) {
    throw ...
}
0
 

Author Comment

by:abstractionz
ID: 9839566
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
 

Author Comment

by:abstractionz
ID: 9839573
thats why i was thinking of throwing my exception when i catch an eofexception
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9839596
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
 

Author Comment

by:abstractionz
ID: 9839610
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
 

Author Comment

by:abstractionz
ID: 9839612
so it's for record numbers that don't exist
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 125 total points
ID: 9839651
>>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
 

Author Comment

by:abstractionz
ID: 9839659
i never thought of that.  nice.  how do i get the filesize?
0
 
LVL 92

Expert Comment

by:objects
ID: 9839669
try
{
   // look for record
}
catch (EOFException ex)
{
   throw new RecordNotFoundException();
}
0
 

Author Comment

by:abstractionz
ID: 9839673
yea that's the code i originally thought of writing
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9839742
It's not particularly good to deliberately cause excepetions
0
 
LVL 92

Expert Comment

by:objects
ID: 9839783
> It's not particularly good to deliberately cause excepetions

It may not be deliberately caused.
0
 

Author Comment

by:abstractionz
ID: 9839787
is there a function to get the total number of bytes in a file?
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 9839807
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9839811
long fileLength = file.length();
0
 

Author Comment

by:abstractionz
ID: 9839836
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 9839862
8-)
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Error trying to install RTMT Win7 5 59
javap not working 8 43
hibernate example using maven 12 41
jboss wildfly 10.1 10 75
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

11 Experts available now in Live!

Get 1:1 Help Now