troubleshooting Question

Handle Exceptions during instantiation

Avatar of rickhill11
rickhill11 asked on
Microsoft DevelopmentC++
28 Comments1 Solution551 ViewsLast Modified:
I have a class that will allocate some memory, open a file, populate a structure, and do some other house keeping.

My current thought is to do something like:

     foo f;  //set 2 flags, one that the memory allocation succeeded, one that there is no open file, so no operations are valid except open()
     if(!"filename")) then do something.
     open() would do all the dirty work, and every method in the class, would first check to see if a valid file is open and ready for i/o.  This to avoid things like
         foo f;
         f.fetch(); //or whatever would throw an error

The class will be in a static library or DLL, and will be used by many different programs in the Windows environment; although, I might, in my copious free time, try to port to Linux.  

Am I going at this correctly?

One last question, in the case of a failure to open, I want the calling program to decide what to do with the error.  The failure to open might even be anticipated, with some recovery routine.   On the other hand when a programmer does
        foo f;
        f.fetch();   there is no file open, this is programmer's error.  What is the best way to handle this?  It should always stop execution.  I would rather not have to make every call part of an if statement as in if(!f.fetch())... or if(f.valid())f.fetch();
Senior Software Engineer (Avast)

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Log in to continue reading
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform for $9.99/mo
View membership options
Unlock 1 Answer and 28 Comments.
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
See how we're fighting big data
The Value of Experts Exchange in My Daily IT Life

Experts Exchange (EE) has become my company's go-to resource to get answers. I've used EE to make decisions, solve problems and even save customers. OutagesIO has been a challenging project and... Keep reading >>


Owner of Outages.IO
Phoenix, Arizona, United States
Member Since 2016
Join a full scale community that combines the best parts of other tools into one platform.
Unlock 1 Answer and 28 Comments.
View membership options
“All of life is about relationships, and EE has made a virtual community a real community. It lifts everyone's boat.”
William Peck

Member since 2004