473,698 Members | 2,156 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Is it possible to catch an exception raised by a member variable?

Hello

I have a class I am using which raises an exception in its constructor
if certain things aren't in place. I can easily create the situation
where an exception is raised.

If I use the create a member variable in a class using this class then
how do I catch the exception?

For now I have defined a member function as a pointer and in my
constructor in my class using the class which raises the exception, I
do a try catch block and do a new object. That works. But is it
possible to do a similar thing using a member variable?

Angus

Sep 27 '07
12 1927
On Sep 28, 4:33 pm, "Chris ( Val )" <chris...@gmail .comwrote:
On Sep 28, 10:52 pm, Pete Becker <p...@versatile coding.comwrote :
On 2007-09-28 05:30:31 -0400, "Chris ( Val )" <chris...@gmail .comsaid:
int main()
{
Base* B;
try {
B = new Base( "Oracle.driver. bar" );
}
catch( const std::exception& e )
{
B->Print();
delete B;
}
std::cin.get();
return 0;
}
I am interested to hear your, and the groups
thoughts on the validity of such a construct.
It really doesn't show anything. Replace the "B = new ..." with "throw
std::exception( );" and you'll probably get the same result. Calling
member functions on uninitialized pointers produces undefined behavior,
so anything you see is as valid as anything else.
I tried a non pointer version, and the results are the same.
Could you show it. If you replace the line with new Base...,
with something like:

Base b( "Oracle.driver. bar" ) ;

then b isn't even in scope in the catch block, and cannot be
accessd.
What I am curious about is at what point does the object actually
cease to exist? (which scope?)
Which object. The object whose destructor exits via an
exception never begins to exist, so the question of when it
ceases to exist is moot.
Is it not possible for it to even be partially constucted to
report such information back?
No. Or rather, it can put information in the exception, which
will propagate up. But if a constructor exits because of an
exception, all fully constructed sub-objects are immediately
destructed, and if the constructor was called as part of a new
expression, the allocated memory is immediately freed.

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

Sep 28 '07 #11
On Sep 29, 8:38 am, James Kanze <james.ka...@gm ail.comwrote:
On Sep 28, 11:30 am, "Chris ( Val )" <chris...@gmail .comwrote:
On Sep 28, 7:03 pm, James Kanze <james.ka...@gm ail.comwrote:

[...]


But you still don't have an object. You can use function try
blocks to remap the exception, or to treat it as a fatal error
(e.g. by calling abort or exit), but you cannot return normally
from the constructor, and the object that was being constructed
will not exist.
I have produced a crude example that will attempt to
prove otherwise:
# include <iostream>
# include <string>
# include <exception>
struct DataSource {
DataSource( std::string ds )
{
if( ds != "Oracle.driver. foo" )
throw "Could not connect to database";
}
};
class Base
{
private:
DataSource Ds;
public:
Base( std::string );
~Base() { std::cout << "Destructin g now...\n"; }
void Print()
{ std::cout << "I am still alive - Please try again.\n"; }
};
Base::Base( std::string ds )
try // function-try block
: Ds( ds ) {}
catch( const char* msg ) {
std::cout << "Exception Caught: \"" << msg << "\"" << '\n';
throw std::exception( );
}
int main()
{
Base* B;
try {
B = new Base( "Oracle.driver. bar" );
}
catch( const std::exception& e )
{
B->Print();

This is undefined behavior, since it accesses an uninitialized
pointer. If you get here, the assign in the try block has never
occured (and there is no Base object).
delete B;
}
std::cin.get();
return 0;
}
I am interested to hear your, and the groups
thoughts on the validity of such a construct.

Totally invalide.
The non-pointer version that I spoke of was
totally wrong. I thought about it after I
posted, amd realised I was just assigning
a new object in the catch block. It was late
and I totally confused myself :-)

Thanks to both you and Peter for clarifying it.

Cheers,
Chris Val

Sep 29 '07 #12
On Sep 29, 12:57 am, Pete Becker <p...@versatile coding.comwrote :
On 2007-09-28 10:54:39 -0400, Pete Becker <p...@versatile coding.comsaid:
On 2007-09-28 10:33:00 -0400, "Chris ( Val )" <chris...@gmail .comsaid:
What I am curious about is at what point does the object actually
cease to exist? (which scope?)
Well, formally, it never existed, because its constructor didn't run to
completion. Mechanically, what happens is that when the exception
escapes from the try block

Sorry, I suspect that should be "from the catch clause". I'm a bit
pressed for time this morning, so I haven't looked it up.
No problem at all, I appreciate the help and example.

Thanks,
Chris Val


Sep 29 '07 #13

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

Similar topics

5
2073
by: Hubert Hermanutz | last post by:
Hi, as yet i have readed in the MSDN about exception handling. The only references that i was founded, described try, catch, finally and throw objects. But I am searching about an occation to catch exceptions central, maybe, the framework call a central function of each class, when there is throw a exception. The reason is, that i do not need try, catch... blocks and therefore the sourcecode will be going to unreadable.
7
1386
by: Tiraman | last post by:
Hi , I am using allot the try catch in my code and the question is if it is good ? it will decrease my performance ? one more question
9
3368
by: Michael MacDonald | last post by:
Does someone have a good site I can visit or explain the use of Try" and Catch foe exception/error handling. What is the logic behind this command and maybe an example would be great!!!! Mike_Mac *** Sent via Devdex http://www.devdex.com *** Don't just participate in USENET...get rewarded for it!
7
1720
by: Sean Kirkpatrick | last post by:
I got caught with my pants down the other day when trying to explain Try...Catch...Finally and things didn't work as I had assumed. Perhaps someone can explain to me the purpose of Finally. I've looked at several texts that I have and none of them address this specific point. If I call some method that throws an exception in my routine Foo, sub foo call bar <- throws an exception do something else <- never get here
4
13681
by: chris | last post by:
Hi, I write some code guarded with exception handling... simplified code look like this... int main(int argc, char* argv){ try{
13
2541
by: Alison Givens | last post by:
....... that nobody knows the answer. I can't imagine that I am the only one that uses parameters in CR. So, my question again: I have the following problem. (VB.NET 2003 with CR) I have a report with a multiple-value discrete value and a rangevalue. The report shows fine in the viewer, but when I hit the export to pdf
28
3835
by: RickHodder | last post by:
I'm getting frustrated with using try...catch with local variables: The code below wont compile in .NET 1.1: I get the following error: "Use of unassigned local variable 'oProcessFileReader' " Is there a way around this error? <code> private void Test(string sFileName) {
8
3396
by: reuce-google | last post by:
Hi folks, I want to store an exception and rethrow it later: CException m_pEc = NULL; // Class variable. try { throw new CMemoryException(); }
28
3801
by: gnuist006 | last post by:
I have some code like this: (if (test) (exit) (do something)) or (if (test)
0
9027
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8895
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 most users, this new feature is actually very convenient. If you want to control the update process,...
0
8861
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7725
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 launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6518
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5860
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4369
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4619
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
2329
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.