473,404 Members | 2,137 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,404 software developers and data experts.

class for database handling errors

Hi Everyone,

I'm currently developing a class for a database, each object of the
class will establish a connection to the database on a remote server
and and all of this happens on the constructor. So there are cases
when the connection can fail during the initial setup in the
constructor and i was wondering how to send this error to the caller.
Is it a good design to throw exceptions from the constructor? or is
there any other alternative?

Thanks in advance ! ! !
Jun 27 '08 #1
8 1530
Rahul wrote:
I'm currently developing a class for a database, each object of the
class will establish a connection to the database on a remote server
and and all of this happens on the constructor. So there are cases
when the connection can fail during the initial setup in the
constructor and i was wondering how to send this error to the caller.
Is it a good design to throw exceptions from the constructor? or is
there any other alternative?
IMHO, don't do anything that can fail in the constructor. Create e.g. a
function called "Connect()" for establishing connections or "Create" for
some general initialization that can fail.
Jun 27 '08 #2
On May 10, 3:14 pm, Aggro <spammerdr...@yahoo.comwrote:
Rahul wrote:
I'm currently developing a class for a database, each object of the
class will establish a connection to the database on a remote server
and and all of this happens on the constructor. So there are cases
when the connection can fail during the initial setup in the
constructor and i was wondering how to send this error to the caller.
Is it a good design to throw exceptions from the constructor? or is
there any other alternative?

IMHO, don't do anything that can fail in the constructor. Create e.g. a
function called "Connect()" for establishing connections or "Create" for
some general initialization that can fail.
Why do you say so? What is the class has a pointer and memory for
which needs to be allocated in the constructor for ensuring that the
object is successfully created?
Jun 27 '08 #3
Rahul wrote:
Hi Everyone,

I'm currently developing a class for a database, each object of the
class will establish a connection to the database on a remote server
and and all of this happens on the constructor. So there are cases
when the connection can fail during the initial setup in the
constructor and i was wondering how to send this error to the caller.
Is it a good design to throw exceptions from the constructor? or is
there any other alternative?
If construction of the object fails and this is an exceptional
condition, then throw an exception.

--
Ian Collins.
Jun 27 '08 #4
On May 10, 4:37 pm, Ian Collins <ian-n...@hotmail.comwrote:
Rahul wrote:
Hi Everyone,
I'm currently developing a class for a database, each object of the
class will establish a connection to the database on a remote server
and and all of this happens on the constructor. So there are cases
when the connection can fail during the initial setup in the
constructor and i was wondering how to send this error to the caller.
Is it a good design to throw exceptions from the constructor? or is
there any other alternative?

If construction of the object fails and this is an exceptional
condition, then throw an exception.

--
Ian Collins.
Ok and what about the following case,

class exc
{
public: exc()
{
throw 5;
};
exc(const exc& ref)
{
throw 5;
}
};
int main()
{
try
{
exc obj; // causes an exception
}
catch(exc obj) // while handling the exception a new copy is
created which in turn throws the exception...
{
...
}
}

What about these situations where exceptions are generated when
exceptions are handled? I thought this is probably why constructors
and destructor shouldn't throw any exceptions at all...
Jun 27 '08 #5
In article <e770d853-b9f1-4898-91f0-
2b**********@v26g2000prm.googlegroups.com>, sa*****@yahoo.co.in says...

[ ... ]
catch(exc obj) // while handling the exception a new copy is
created which in turn throws the exception...
{
...
}
}

What about these situations where exceptions are generated when
exceptions are handled? I thought this is probably why constructors
and destructor shouldn't throw any exceptions at all...
IMO, you've picked up the wrong lesson here. The lesson should be
related to exception objects, not to ctors and/or dtors. First of all,
absent some _really_ good reason to do otherwise (which I've yet to
encounter) an exception handler should be written to receive a reference
to a (possibly const) object.

Second, exception objects should generally use fairly minimal resources
themselves in any case -- if (for example) an exception arises because
you've run out of available memory, an exception object that needs
megabytes of memory probably won't work -- and whether it attempts to
allocate that memory in its ctor or somewhere else makes no real
difference.

The language doesn't make any categorical requirements about exception
objects separate from other objects. Nonetheless, well-designed
exception classes bear little resemblance to well-designed classes of
other sorts.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jun 27 '08 #6

>On May 10, 4:37 pm, Ian Collins <ian-n...@hotmail.comwrote:
>Rahul wrote:
Hi Everyone,
I'm currently developing a class for a database, each object of the
class will establish a connection to the database on a remote server
and and all of this happens on the constructor. So there are cases
when the connection can fail during the initial setup in the
constructor and i was wondering how to send this error to the caller.
Is it a good design to throw exceptions from the constructor? or is
there any other alternative?

If construction of the object fails and this is an exceptional
condition, then throw an exception.

--
Ian Collins.

Ok and what about the following case,

class exc
{
public: exc()
{
throw 5;
};
exc(const exc& ref)
{
throw 5;
}
};
int main()
{
try
{
exc obj; // causes an exception
}
catch(exc obj) // while handling the exception a new copy is
created which in turn throws the exception...
{
...
}
}
this doesn't catch the exception..
>
What about these situations where exceptions are generated when
exceptions are handled? I thought this is probably why constructors
and destructor shouldn't throw any exceptions at all...
Jun 27 '08 #7
On 10 mai, 11:58, Rahul <sam_...@yahoo.co.inwrote:
I'm currently developing a class for a database, each object of the
class will establish a connection to the database on a remote server
and and all of this happens on the constructor. So there are cases
when the connection can fail during the initial setup in the
constructor and i was wondering how to send this error to the caller.
Is it a good design to throw exceptions from the constructor? or is
there any other alternative?
Normally, the preferred solution when a constructor cannot
create the object correctly is for it to raise an exception.
This case is, however, a somewhat special case, since you (and
the client code) also has to be prepared to handle the loss of a
connection---this means that you have to be able to deal with an
object without a valid connection. Raising an exception might
still be the correct solution (including raising one if you
detect a missing connection later), but in this case, it doesn't
free you from having to deal with an object which might not have
a valid connection. Regardless of how you notify the client
code, you still have to maintain some sort of internal state as
to whether there is a connection or not, and check it before
each operation (or at least ensure that the operation fails if
there is no valid connection).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #8
On 10 mai, 15:19, Jerry Coffin <jcof...@taeus.comwrote:

[...]
First of all,
absent some _really_ good reason to do otherwise (which I've yet to
encounter) an exception handler should be written to receive a reference
to a (possibly const) object.
I have:-). It's a special case, but in some applications, where
it would normally make sense to call exit() somewhere deep in
call hierarchy, I adopt the convention of throwing an int (the
return code) instead; main catches int and returns. In other
words, I call exit, but only after having executed all of the
intermediate destructors. And of course, there's no point in
catching int with a reference.

(For the rest, I agree with all you've said. I just thought I'd
mention this one special case.)

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
Jun 27 '08 #9

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

Similar topics

12
by: Christian Christmann | last post by:
Hi, assert and error handling can be used for similar purposes. When should one use assert instead of try/catch and in which cases the error handling is preferable? I've read somewhere that...
5
by: news | last post by:
Well, I wrote my first PHP class today. Yeah! But to get it to work, in each function within the class I have to repeat the database connection lines, and that just seems redundant; there has to...
6
by: Tappy Tibbons | last post by:
I do not know exactly how to explain what I am asking for, but here goes... Say I have a simple set of classes: ========= Public Class clsPerson Public FirstName As String Public LastName As...
40
by: Jeff | last post by:
I have a system on a network and want to determine if anyone is currently connected to the back-end files. An interesting twist is that I have noticed that some users can be connected (have the...
17
by: shineofleo | last post by:
Here is the situation: I wrote a VB programm, which stores all the information in a single Access database file using jet engine. It worked well, however one of my customs reported that there was...
21
by: Daz | last post by:
Hi everyone. I am trying to create an extension of the mysqli class within PHP, and I am finding it quite difficult. I am fairly new to PHP classes, and decided to give them a go. Here's what I...
49
by: Ben Voigt [C++ MVP] | last post by:
I'm trying to construct a compelling example of the need for a language feature, with full support for generics, to introduce all static members and nested classes of another type into the current...
2
by: Omar Abid | last post by:
Reason of this project: Error handling is one of the most difficult thing that may afford a programmer. It isn't as easy as you think and handling errors in a program some time can make errors...
55
by: tonytech08 | last post by:
How valuable is it that class objects behave like built-in types? I appears that the whole "constructor doesn't return a value because they are called by the compiler" thing is to enable...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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.