473,993 Members | 2,122 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Exceptions creating Exceptional Problems!

Hi all

I'm facing a strange problem in C++ exceptions.

If an error occurs in a class, throw() is used. But whenever that
happens, the object is destroyed.

In a nutshell, accessing the object in which exception occured is not
being possible in catch block.

Either i'm interpreting the things wrongly, or is it a limitation...

Please suggest some workaround AND|OR clear my mis-interpretation.

A more detailed version of my question can be found at my blog...
http://makuchaku.blogspot.com/2005/0...ceptional.html
Do reply asap...

Thanks,
makuchaku

Jul 22 '05 #1
5 1161
"makuchaku" <ma********@gma il.com> wrote in message
news:11******** *************@c 13g2000cwb.goog legroups.com...
I'm facing a strange problem in C++ exceptions.

If an error occurs in a class, throw() is used. But whenever that
happens, the object is destroyed.

In a nutshell, accessing the object in which exception occured is not
being possible in catch block.

Either i'm interpreting the things wrongly, or is it a limitation...

Please suggest some workaround AND|OR clear my mis-interpretation.

A more detailed version of my question can be found at my blog...
http://makuchaku.blogspot.com/2005/0...ceptional.html
Do reply asap... The code was short enough that you could have posted it here:
#include<...>
void main()
{
try
{
Socket object(); The above is a function declaration. I guess you meant:
Socket object; object.do_somet hing_else();
}catch(SockExce ption err)
{
err.getCode(); //works
err.getMessage( ); //works
//object.cannot_a ccess_this(); <----- this is troublesome! For that to work, 'object' need to be declared outside of/prior to
the try block:
Socket object;
try {
..... }
}


Of course this implies that if you cannot access the object in a
catch clasue if its constructor has thrown an exception. If the
constructor did not execute successfully, there is no object to
be accessed.

Eventually, you may want to use two-step initialization: a
non-throwing constructor that creates an instance in a fail-safe
state, and a separate function to attempt the creation of a
resource (or connection, in this instance).

This very much makes sense to me.
Do you consider this to be a practical problem ?

Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #2
your idea can be a possible solution.
I also found another way!
int main()
{
Socket *s;
try
{
s = new Socket(ERRORNEO US_VALUE);
}catch(...)
{
s->memberFunction ();
}
}

atleast this works. Now its upto me, how do i design my class.
anyways, thanks for your help.

makuchaku

Jul 22 '05 #3
"makuchaku" <ma********@gma il.com> wrote in message
news:11******** *************@c 13g2000cwb.goog legroups.com...
your idea can be a possible solution.
I also found another way!

int main()
{
Socket *s;
try
{
s = new Socket(ERRORNEO US_VALUE);
}catch(...)
{
s->memberFunction ();
}
}
atleast this works. Now its upto me, how do i design my class.


Here you have traded a compile-time error for Undefined Behavior!
If the above code works, it is by accident - and it will fail
(or erase your HD etc) with a different compiler setting or some
other change of circumstances.
Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Jul 22 '05 #4
hi,
you may be correct, but what if i'm in a parent>child>su b-child
environment & want that after error in sub-child class, the child class
should not die/end.

makuchaku

Jul 22 '05 #5

makuchaku wrote:
hi,
you may be correct, but what if i'm in a parent>child>su b-child
environment & want that after error in sub-child class, the child class should not die/end.

makuchaku


Then you should do as I. Vecerina suggested. (Re)build the constructor
of Socket so that it will not throw (user defined exceptions that is)
and write an initializer function that is allowed to throw.

Jul 22 '05 #6

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

Similar topics

24
2517
by: mag31 | last post by:
Is there any way to find out if a particular .net function will throw an exception without first generating the exception? I am using structured exception handling i.e. try catch finally blocks with a top level catch all for Exception. However, I would like to be able to catch most .net exceptions when they are generated. I would then be able to generate a valuable exception message and do something about it!!! Hence the question above....
9
2352
by: Gianni Mariani | last post by:
I'm involved in a new project and a new member on the team has voiced a strong opinion that we should utilize exceptions. The other members on the team indicate that they have either been burned with unmaintainable code (an so are now not using exceptions). My position is that "I can be convinced to use exceptions" and my experience was that it let to code that was (much) more difficult to debug. The team decided that we'd give...
10
1568
by: Jakob Bieling | last post by:
Hi, somehow the prejudice of exceptions being rather slow (compared to, ie. returning an error value and checking that) keeps sticking around .. at least around me. I guess this is also why I refrained from using them often. But are they 'slow' in general? I guess it also depends on how, when and where you use them. What I am looking for is a sort of guideline that explains where exceptions are approriate and where they are not. The...
6
2844
by: RepStat | last post by:
I've read that it is best not to use exceptions willy-nilly for stupid purposes as they can be a major performance hit if they are thrown. But is it a performance hit to use a try..catch..finally block, just in case there might be an exception? i.e. is it ok performance-wise to pepper pieces of code with try..catch..finally blocks that must be robust, in order that cleanup can be done correctly should there be an exception?
14
3505
by: dcassar | last post by:
I have had a lively discussion with some coworkers and decided to get some general feedback on an issue that I could find very little guidance on. Why is it considered bad practice to define a public member with a return type that is derived from System.Exception? I understand the importance of having clean, concise code that follows widely-accepted patterns and practices, but in this case, I find it hard to blindly follow a standard...
8
2271
by: cat | last post by:
I had a long and heated discussion with other developers on my team on when it makes sense to throw an exception and when to use an alternate solution. The .NET documentation recommends that an exception should be thrown only in exceptional situations. It turned out that each of my colleagues had their own interpretation about what an "exceptional situation" may actually be. First of all, myself I’m against using exceptions extensively,...
1
2409
by: Anonieko | last post by:
Understanding and Using Exceptions (this is a really long post...only read it if you (a) don't know what try/catch is OR (b) actually write catch(Exception ex) or catch{ }) The first thing I look for when evaluating someone's code is a try/catch block. While it isn't a perfect indicator, exception handling is one of the few things that quickly speak about the quality of code. Within seconds you might discover that the code author...
6
2062
by: Liming | last post by:
Hi, In a typical 3 tier model (view layer, busines layer and data access layer) where do you handle your exceptions? do you let it buble up all the way to the .aspx pages or do you handle it in your business layer and/or data access layer? suppose in my data access layer, I provide try and catch, log the exception and re-throw it back to business layer, then in yoru business layer, what do you do? throw it back to the code behind or...
2
1766
by: Jeff | last post by:
Hey ..NET 2.0 I'm developing an application (n-tier, data acces layer (DAL), bizlayer and presentation layer) If for example in the DAL a stored procedure call returns no rows then I want an exception to be thrown. So I created an custom exception called NoRowsException and throws it.
0
10417
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 usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10234
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11936
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
11502
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
11740
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
10172
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
8560
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
6674
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
3850
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.