473,396 Members | 2,111 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.

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 1680

"Steve" <st*******@hotmail.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******@mkwahler.net> wrote in message
news:a2******************@newsread1.news.pas.earth link.net...

"Steve" <st*******@hotmail.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_does_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
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...
4
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...
3
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...
4
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...
4
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...
1
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)...
4
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
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
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...
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
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
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...
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.