473,545 Members | 2,042 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Handling Class Construction Errors

Hi Folks,

Sorry for this stupid question, but how do you handle errors during class
construction. In other words, if I have a class that loads a file, and
during loading, an error occurs, how do you deal with this in respect of
releasing the resources that would have been allocated if the load was
successful?

What I am trying to achieve is, if I call my class with new, how do I return
NULL if the construction fails (so I can check for fails). Also, if I call
the class without new, ie 'myclassname myclass(param)' , how would you deal
with this in event of failure.

I hope my question is clear, I know what I want to ask, but not sure if I've
asked it properly.

Also, are answers to questions like this in Stroustrup's book?

Thanks for your time,
Steve.
Jul 22 '05 #1
2 1690

"Steve" <st*******@hotm ail.com> wrote in message
news:ZGtnc.114$ Ne7.77@newsfe1-win...
Hi Folks,

Sorry for this stupid question, but how do you handle errors during class
construction.
If your object cannot complete construction, throw an
exception.
In other words, if I have a class that loads a file, and
during loading, an error occurs, how do you deal with this in respect of
releasing the resources that would have been allocated if the load was
successful?
If something 'would have been' allocated, that doesn't mean it *was*.
Nothing need be done.

What I am trying to achieve is, if I call my class with new, how do I return NULL if the construction fails
You don't. A constructor cannot return a value. Also note that
if operator 'new' fails, it will throw an exception.
(so I can check for fails).
Catch the exception(s).
Also, if I call
the class without new, ie 'myclassname myclass(param)' , how would you deal
with this in event of failure.
If the object cannot be constructed, throw an exception.

I hope my question is clear, I know what I want to ask, but not sure if I've asked it properly.

Also, are answers to questions like this in Stroustrup's book?


Yes.

-Mike
Jul 22 '05 #2

"Mike Wahler" <mk******@mkwah ler.net> wrote in message
news:a2******** **********@news read1.news.pas. earthlink.net.. .

"Steve" <st*******@hotm ail.com> wrote in message
news:ZGtnc.114$ Ne7.77@newsfe1-win...
Hi Folks,

Sorry for this stupid question, but how do you handle errors during class construction.


If your object cannot complete construction, throw an
exception.


This is normally the best thing to do.

However an alternative is to define an 'error state' for your object and
leave the object in that state if an error occurs.

This is what ifstream does for instance.

ifstream f("file_that_do es_not_exist");
if (!f.is_open())
cerr << "could not open file\n";

But like Mike says, throwing an exception is normally the better option. I
guess the likelihood of the error occurring would influence which method you
choose.

john
Jul 22 '05 #3

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

Similar topics

9
3189
by: Hans-Joachim Widmaier | last post by:
Hi all. Handling files is an extremely frequent task in programming, so most programming languages have an abstraction of the basic files offered by the underlying operating system. This is indeed also true for our language of choice, Python. Its file type allows some extraordinary convenient access like: for line in open("blah"):...
4
1441
by: pixelbeast | last post by:
hi, In the following code, I would like not to have to declare the constructors in bar ( either the default or the (int a) constructor ). When I remove them, bar has no idea of construction with an int, so I get compile errors. I currently have to declare them for every class inheriting foo, and it would be much nicer just with the init. Is...
3
4768
by: Steve - DND | last post by:
I was wondering if anyone can point me to some articles or practices they use for dealing with errors in their applications; particularly in an n-tier design. I am trying to find one way to conveniently handle both soft and hard errors, primarily for display to the end user. One method I have considered is rethrowing exceptions. With this...
4
2517
by: aaj | last post by:
Hi all I have an automated application, that runs in the middle of the night. If certain 'non system' errors occur (things like malformed files, missing files etc..), I send an automatic Email and write a record to the database. This is handled in a class. When these errors occur, once Emailed and written I want to just end the App,...
4
7602
by: James Radke | last post by:
Hello, I am looking for guidance on best practices to incorporate effective and complete error handling in an application written in VB.NET. If I have the following function in a class module (note that this class module represents the business layer of code NOT the gui layer): Public Function Test(ByVal Parm1 As Integer, ByVal Parm2 As...
1
1152
by: Eric Newton | last post by:
Given the following code, Public class ProgressFormManager Public Sub BeginInvoke(ByVal method As , ByVal args() As Object) Dim asyncCallback As New System.Threading.WaitCallback(method) System.Threading.ThreadPool.QueueUserWorkItem(asyncCallback, args) End Sub .... End Class
4
2014
by: robinsand | last post by:
Header File: car.h #if !defined CAR_H #define CAR_H enum TCarType { ctEconomy = 1, ctCompact, ctStandard, ctFullSize, ctMiniVan, ctSUV }; class Car { public: Car();
3
1419
by: desktop | last post by:
I have a class that contains: class MyArray { public: MyArray(int a) : num(a){} void generate() { ip = new int; }
8
1539
by: Rahul | last post by:
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...
0
7478
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7668
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7923
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7437
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7773
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
5984
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
4960
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3466
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3448
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.