By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,129 Members | 1,245 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,129 IT Pros & Developers. It's quick & easy.

how do you make this?

P: n/a
Hello.
I have a class where the object are created with the data retrieved
from a db.

The data retrieved is an image and when this data doesn't exist the
constructor must generate it.
well, i have doubts to do this.

I think that i have two ways for this. The first is create an exception
that it is throw when the data are not and in the "catch sentence" the
data would be generated and the second is using an "if sentence"
what way is better? how is more elegant?

Best regards
M.

Dec 19 '06 #1
Share this Question
Share on Google+
3 Replies


P: n/a

Manuel wrote:
Hello.
I have a class where the object are created with the data retrieved
from a db.

The data retrieved is an image and when this data doesn't exist the
constructor must generate it.
well, i have doubts to do this.

I think that i have two ways for this. The first is create an exception
that it is throw when the data are not and in the "catch sentence" the
data would be generated and the second is using an "if sentence"
what way is better? how is more elegant?

Best regards
M.
No sir, exceptions are used for exceptional circumstances to handle
errors or discrepencies.
One does not use exceptions to conditionally create some instance of a
type.
In your case, the class in question should use 3 constructors, one
defaut ctor, one parametized ctor and a copy ctor. Which ctor gets
invoked depends on the arguements passed to it, if any.

class Image { ... };

class Data
{
Image m_image;
public:
Data() : m_image() { } // def ctor
Data( Image& image ) : m_image(image) { } // param ctor
Data( const Data& copy ) // copy ctor
{
m_image = copy.m_image;
}
};

How you integrate that in your code at this point is information i
don't have.

Dec 19 '06 #2

P: n/a
Manuel wrote:
Hello.
I have a class where the object are created with the data retrieved
from a db.

The data retrieved is an image and when this data doesn't exist the
constructor must generate it.
well, i have doubts to do this.

I think that i have two ways for this. The first is create an
exception that it is throw when the data are not and in the "catch
sentence" the data would be generated and the second is using an "if
sentence"
what way is better? how is more elegant?
If you dig around for "exception vs error code", you'll probably see
more than one opinion. I think that exceptions are not good for what
you need here. Improper data happen all the time, and it should be
treated as a normal situation and not an exceptional one. If you have
to design a mechanism to respond to incorrectly formatted or simply
wrong data, it's another sign that exceptions are not for that. In
an exceptional situation you usually just log what happened and bail
out. It is more likely that you cannot work around an exception. If
you can think of a work-around, you should probably employ error code
mechanism (the "if sentence").

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 19 '06 #3

P: n/a


On Dec 19, 8:56 pm, "Victor Bazarov" <v.Abaza...@comAcast.netwrote:
Manuel wrote:
Hello.
I have a class where the object are created with the data retrieved
from a db.
The data retrieved is an image and when this data doesn't exist the
constructor must generate it.
well, i have doubts to do this.
I think that i have two ways for this. The first is create an
exception that it is throw when the data are not and in the "catch
sentence" the data would be generated and the second is using an "if
sentence"
what way is better? how is more elegant?If you dig around for "exception vs error code", you'll probably see
more than one opinion. I think that exceptions are not good for what
you need here. Improper data happen all the time, and it should be
treated as a normal situation and not an exceptional one. If you have
to design a mechanism to respond to incorrectly formatted or simply
wrong data, it's another sign that exceptions are not for that. In
an exceptional situation you usually just log what happened and bail
out. It is more likely that you cannot work around an exception. If
you can think of a work-around, you should probably employ error code
mechanism (the "if sentence").

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Well, My doubt is not around for "exception vs error code", i think.
the question isn't that it retrieved improper data.

The question is that it can or can not be the image data in the db. In
case it isn't data, the constructor must generate it.

after i read your mail and the Salf_peter's mail I think that i must
add other way: Make two constructors, one that has the parameter data
and other that hasn't it and it must connect to db to read de image
data.
This method implies that in the class where it is made an instance of
the primary class must connect it to db too. i don't know if it is
correct because the data access object is used in more places

i don't decide that is the better choice but I incline by the last
option

Best regards
M.

Dec 19 '06 #4

This discussion thread is closed

Replies have been disabled for this discussion.