3

I am having troubles with dependencies between two classes. The problem is the following:

I got two classes, Timestamp and Exception. Exception is abstract and all possible exceptions derive from this abstract class. Every exception has a timestamp to tell when the exception was thrown. So exceptions need to include (in the language i am using it is called import) the timestamp class. But wen working with timestamps errors can occur, so that exceptions are thrown. Therefor the timestamp class has to import the exception classes.

And there is my cyclic dependency. Now my actual question is (and this is why it is independent from the language): What would be a proper design in such a case to avoid cyclic dependencies? I fail to solve this problem as I cannot figure out a solution to have this classes as independent as they are now but without the cyclic dependency.

0

2 Answers 2

11

I'd drop the whole "Exception-has-timestamp-property" idea. Exceptions should indicate exceptional behavior and nothing else. Perhaps you want to do some logging? It makes more sense for the logger to keep track of the timestamps and pair them with their appropriate exceptions.

Ask yourself what the job of an exception is. Obviously, an exception's job is to get thrown. Does this depend on when it gets thrown? Nope. Does the exception care about this at all? Nope. Does something else care? Yes, the exception logger. But, since the logger is the one who cares about timestamps, the logger should also get those timestamps, and do stuff with them. It's part of its job, after all. And, in a proper design, the logger does not outsource parts of his work to exceptions.

Cyclic dependency solved.

Sign up to request clarification or add additional context in comments.

5 Comments

Actually I ain't sure if he is talking about this kind of exceptions. If he is, your answer is definitely more valuable then mine.
I think he's referring to exceptions as in... exceptions - "But wen working with timestamps errors can occur, so that exceptions are thrown." That is, exceptions that get thrown when exceptional behavior (such as a failure) is encountered. That's what the author is letting us understand, anyway.
I find your suggestion very good. And yes, the way I am using exceptions is just... Exceptions. They are thrown and maybe catched (or maybe not). So there is actually no Exception Logger, but if an exception is catched there is a way to Log it. But to do that it is converted into a Message which is then logged from a debugmanager. So the exception should know when it was thrown. Or do you think it would be better to not just throw an exception but instead let an exception manager throw the exception, using it like a factory?
Why not propagate the exception until it reaches logging point? Get the timestamp value when it reaches said point. The time it takes for the exception to propagate should not matter, it's too small. In pretty much any framework (say, .NET or Java's), exceptions behave just like this, and logging was seldom a problem. Anything more complicated than this seems like overkill to me.
If you do add some extra (manual) delay during propagation, then the object that calls the delay should also be able to mark the time right before the delay as the original timestamp. In this case, you could make your debugmanager listen to events from such objects. Through events you'd pass each exception and its original timestamp. Eventually, you could also pass the delay. Not sure if this is your case though.
1

Good Question, especially that you want to avoid the cyclic dependency instead of just getting it to work. But consider that cyclic dependencies aren't bad practice in general. In your case, I is reasonable to make use of both class from each other.

Therefore many languages allow you to define class prototypes. I guess you are using python, though in C++ this looks this way.

class Timestamp;

You can now define the Exception class and use Timestamp object as members. Of course you cannot use it's methods since they aren't defined yet. But say if the Timestamp constructor initialized it's instance to the current time, you may not need to access members from the Exception class.

class Exception
{
    // ...
    Timestamp timestamp;
};

Later on you define the whole Timestamp class.

class Timestamp
{
    Timestamp()
    {
        // initialize to current time
    }
};

But note that coupling two classes this way makes them very dependent. You cannot use one of them without the other.

So you may want to drop the idea to use a Timestamp class for exceptions just to store the time of their occurrence. I guess fetching the actual time stamp just takes you some lines on most operating systems. So if you don't need the whole Timestamp class I suggest to duplicate the code to get the current time. This may be the most practical approach for example if you want to write exceptions along with their time in a log file.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.