473,396 Members | 1,689 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

better design for catching errors

Hello,

I have a program that reads from a file at several different places. I
want to find when the file hits the EOF. At this point I want to tell
the calling method that this has happened so that the program can move
onto the next phase.

My class hierarchy is something like:
main() -> class1 -> class1A -> class1a
-> class1B
-> class1C
-> class2 -> class2A -> class2a
-> class3

Class1a, class1B and class1C all read the same file.
When the file is finished with, control must be passed to class3.

At present I pass this message by using 'return 1;' from the read
method of classes 1a, 1B, 1C and checking the status of this method
using for example:
if (class1a.readMethod() == 1) return 1; // called by
class1A.someMethod();

The problem I am facing is that the calling class (class1A above),
which checks the status, is not the same class as that which
determines whether the program continues beyond the file read
(class1).
Therefore, as shown below, I have to pass this message up several
classes from the read method in the bottom class to the control method
in the top class, i.e. continually returning 1 if the method of the
subclass returned 1.

Also, since I read the file from several different classes, I must do
this message passing from several points through the hierarchy.

main()
{
while(true)
{
if (class1.someMethod() == 1) break;
}
class3.someMethod();
};

int class1.someMethod()
{
if (class1A.someMethod() == 1) return 1;
};

int class1A.someMethod()
{
if (class1a.someMethod() == 1) return 1;
if (class1B.someMethod() == 1) return 1;
if (class1C.someMethod() == 1) return 1;
};

As you can see, this is very messy.

Can anyone suggest a better design for this kind of system?
Perhaps a error handling class, called if EOF is reached, which
returns control to a strategic point? I just can't see it...

thanks
Fred
Jul 22 '05 #1
4 1331

"fred" <jc****@space.qinetiq.com> wrote in message
news:19**************************@posting.google.c om...
Hello,

I have a program that reads from a file at several different places. I
want to find when the file hits the EOF. At this point I want to tell
the calling method that this has happened so that the program can move
onto the next phase.

[snip]

As you can see, this is very messy.

Can anyone suggest a better design for this kind of system?
Perhaps a error handling class, called if EOF is reached, which
returns control to a strategic point? I just can't see it...


Didn't you ask this before?

Throw an exception, returning control to a strategic point is exactly what
exceptions are designed for.

john
Jul 22 '05 #2
Hi Fred,

What you need to do is that let the calling class subscribe for EOF event.
When the EOF is hit, call back on it to inform that EOF is reached.

Now you calling program can write the code inside that call back method to
execute its behavior.

regards,
Shashank

fred wrote:
Hello,

I have a program that reads from a file at several different places. I
want to find when the file hits the EOF. At this point I want to tell
the calling method that this has happened so that the program can move
onto the next phase.

My class hierarchy is something like:
main() -> class1 -> class1A -> class1a
-> class1B
-> class1C
-> class2 -> class2A -> class2a
-> class3

Class1a, class1B and class1C all read the same file.
When the file is finished with, control must be passed to class3.

At present I pass this message by using 'return 1;' from the read
method of classes 1a, 1B, 1C and checking the status of this method
using for example:
if (class1a.readMethod() == 1) return 1; // called by
class1A.someMethod();

The problem I am facing is that the calling class (class1A above),
which checks the status, is not the same class as that which
determines whether the program continues beyond the file read
(class1).
Therefore, as shown below, I have to pass this message up several
classes from the read method in the bottom class to the control method
in the top class, i.e. continually returning 1 if the method of the
subclass returned 1.

Also, since I read the file from several different classes, I must do
this message passing from several points through the hierarchy.

main()
{
while(true)
{
if (class1.someMethod() == 1) break;
}
class3.someMethod();
};

int class1.someMethod()
{
if (class1A.someMethod() == 1) return 1;
};

int class1A.someMethod()
{
if (class1a.someMethod() == 1) return 1;
if (class1B.someMethod() == 1) return 1;
if (class1C.someMethod() == 1) return 1;
};

As you can see, this is very messy.

Can anyone suggest a better design for this kind of system?
Perhaps a error handling class, called if EOF is reached, which
returns control to a strategic point? I just can't see it...

thanks
Fred


Jul 22 '05 #3
"John Harrison" <jo*************@hotmail.com> wrote in message news:<2h************@uni-berlin.de>...
[snip]
Throw an exception, returning control to a strategic point is exactly what
exceptions are designed for.


Exceptions can be used that way. But generally, the "designed for"
purpose of exceptions is richer than that. Usually, what you want
to use an exception for is what its name suggests, to deal with
the siutations where the contract of the interface can't be met.

So, if the interface of a class specifies that it looks for the
end of file, and returns a value through the regular interface
indicating that, then probably you don't want that as an exception.
But if the interface specs that it is not supposed to encounter
the end of the file during operation, and can't succeed if it
does, then it should throw an exception in that case.

So, to restate the rule: If the class completes a task within
the specified rules, it should indicate that through the regular
interface. If it can't satisfy the requirements specified, then
it should throw an exception.

This is not a "hard and fast" rule, not a religious decree.
There are times and places to break this rule, as with many
software design rules. But probably the original poster's
problems arise from crummy design rather than lack of use
of exceptions.
Socks
Jul 22 '05 #4

"fred" <jc****@space.qinetiq.com> wrote in message
news:19**************************@posting.google.c om...
Hello,

I have a program that reads from a file at several different places. I
want to find when the file hits the EOF. At this point I want to tell
the calling method that this has happened so that the program can move
onto the next phase.


Sounds remotely like a parsing task. See
http://www.boost.org/libs/spirit/index.html. As the file is read, objects
are constructed. Constructors can calculate and set their own state
variables.

What is a higher level description of what you are trying to do?

Jeff F
Jul 22 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

3
by: fernandez.dan | last post by:
I'm still learning how to use Object Oriented concepts. I'm have a basic question dealing with design. I have two classes that deal with I/O pertaining to network and usb that inherit from an...
7
by: cmay | last post by:
FxCop complains every time I catch System.Exception. I don't see the value in trying to catch every possible exception type (or even figuring out what exceptions can be caught) by a given block...
11
by: chopsnsauce | last post by:
Here's the example: Dim frm As New FORM1 Try frm.show Catch ex As Exception msgbox ex.message
39
by: windandwaves | last post by:
Hi Folk I have to store up to eight boolean bits of information about an item in my database. e.g. with restaurant drive-through facility yellow windows
1
by: mae2x | last post by:
Web design High-quality web & graphics Designs. That eye catching multimedia and web design developments highly skilled web programmers and designers that have extensive experience in website...
8
by: Simon Willison | last post by:
Hi all, I have an API design question. I'm writing a function that can either succeed or fail. Most of the time the code calling the function won't care about the reason for the failure, but...
4
by: John Pye | last post by:
Hi all I have some C code that is giving me some 'nan' values in some calculations. The C code is wrapped using SWIG to give me a Python module that I am then exercising through a unittest...
7
by: apollonius2 | last post by:
Greetings, I have been working on a little project today to help me better understand classes in Python (I really like Python). I am a self taught programmer and consider myself to fall in the...
3
by: john | last post by:
I wrapped some fortran code using F2PY and need to be able to catch fortran runtime errors to run the following: # "grid" is a wrapped fortran module # no runtime errors incurred when run with...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.