473,379 Members | 1,190 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,379 software developers and data experts.

Throwing Errors

Hello

I'm struggling with the throw and catch exception system. As I understood
it, when you throw an error, that the stack is searched for a matching
catch, but I'm having difficult finding where to put a catch that can be
found by all the methods in a class. Can't I include a try{}catch in a
constructor and then expect it to be found by throws which are littered
throughout my object?

Also, if I have an class object within my class in a hasa relationship,
can the included object reach throw things to the enclosed object, or is
that another space altogether unless real inheritance is used?

Ruben

--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Sep 16 '08 #1
8 1555
Ruben wrote:
I'm struggling with the throw and catch exception system. As I understood
it, when you throw an error, that the stack is searched for a matching
catch, but I'm having difficult finding where to put a catch that can be
found by all the methods in a class.
There's no way to create a 'catch' that will catch exceptions thrown "by
all the methods in a class". The search for the matching handler is not
in any way tied to the type definitions or their relationships.

The term "stack" (the one that's searched for the handler) refers to the
run-time execution stack the way it looked at the moment when the
exception was thrown, i.e. "stack" is the sequence of nested function
contexts leading from 'main' to the 'throw' in question.
Can't I include a try{}catch in a
constructor and then expect it to be found by throws which are littered
throughout my object?
No, that's completely unrelated.
Also, if I have an class object within my class in a hasa relationship,
can the included object reach throw things to the enclosed object, or is
that another space altogether unless real inheritance is used?
Same thing. The search for the matching handler is not in any way tied
to the data structures. It has nothing to do with inheritance or any
other relationships, like 'has-a'. As stated above, it is defined by the
run-time sequence of function call contexts.

--
Best regards,
Andrey Tarasevich
Sep 16 '08 #2
On Tue, 16 Sep 2008 12:50:26 -0700, Andrey Tarasevich wrote:
The term "stack" (the one that's searched for the handler) refers to the
run-time execution stack the way it looked at the moment when the
exception was thrown, i.e. "stack" is the sequence of nested function
contexts leading from 'main' to the 'throw' in question.
Well that is where I'm confused. Why isn't the constructor in the stack
of the object.

Ruben

--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Sep 16 '08 #3
On Tue, 16 Sep 2008 12:50:26 -0700, Andrey Tarasevich wrote:
>
Same thing. The search for the matching handler is not in any way tied to
the data structures. It has nothing to do with inheritance or any other
relationships, like 'has-a'. As stated above, it is defined by the
run-time sequence of function call contexts.
but if one object is calling anoother object why is it not in the stack

main calls a construstor which instantates an object which calls another
constructor which instantates another object which causes an error.

Ruben
--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Sep 16 '08 #4
Ruben wrote:
On Tue, 16 Sep 2008 12:50:26 -0700, Andrey Tarasevich wrote:
>The term "stack" (the one that's searched for the handler) refers to the
run-time execution stack the way it looked at the moment when the
exception was thrown, i.e. "stack" is the sequence of nested function
contexts leading from 'main' to the 'throw' in question.

Well that is where I'm confused. Why isn't the constructor in the stack
of the object.
a) There is no "stack of the object".

b) The constructors local storage is part of stack while the constructor is
running. Once it is done, the object is constructed and the constructors
local storage is removed from the stack. Exceptions thrown at that point
cannot be caught by a try-catch block in the constructor since the
constructor is no longer part of the stack.
Best

Kai-Uwe Bux
Sep 17 '08 #5
On Tue, 16 Sep 2008 19:59:19 -0400, Kai-Uwe Bux wrote:
Exceptions thrown at
that point cannot be caught by a try-catch block in the constructor since
the constructor is no longer part of the stack.
Yes - I can see that. It makes it seem that throw, try and catch are
very limited tools and I just don't see how it improves error handling.

Other than wrapping main into a try block, it seems hard to get anything
useful out of it in terms of organizing exception handling.
Any even still, an class object called from and in a has relationship
with a class oject I would think would still be in the stack of the
claling object.

Ruben
--
http://www.mrbrklyn.com - Interesting Stuff
http://www.nylxs.com - Leadership Development in Free Software

So many immigrant groups have swept through our town that Brooklyn, like Atlantis, reaches mythological proportions in the mind of the world - RI Safir 1998

http://fairuse.nylxs.com DRM is THEFT - We are the STAKEHOLDERS - RI Safir 2002

"Yeah - I write Free Software...so SUE ME"

"The tremendous problem we face is that we are becoming sharecroppers to our own cultural heritage -- we need the ability to participate in our own society."

"I'm an engineer. I choose the best tool for the job, politics be damned.<
You must be a stupid engineer then, because politcs and technology have been attached at the hip since the 1st dynasty in Ancient Egypt. I guess you missed that one."

© Copyright for the Digital Millennium

Sep 17 '08 #6
Ruben wrote:
> Exceptions thrown at
that point cannot be caught by a try-catch block in the constructor since
the constructor is no longer part of the stack.

Yes - I can see that. It makes it seem that throw, try and catch are
very limited tools and I just don't see how it improves error handling.
No, they are not very limited and they do improve error handling.
Other than wrapping main into a try block, it seems hard to get anything
useful out of it in terms of organizing exception handling.
Most likely because you are trying to think about C++ program in terms
of some alternative data/execution model, which in fact has no relevance
to C++ at all. It might be an interesting exercise, but with very little
immediate practical value.
Any even still, an class object called from and in a has relationship
with a class oject I would think would still be in the stack of the
claling object.
There's no such thing as "an object calling another object" in C++.
Objects are data. Data cannot "call" anything and cannot be "called"
from anywhere. The only things that can be called are _functions_. It
could be either standalone functions, or member functions - doesn't
matter. But it is still about functions, not objects. The program
execution flow in C++ is a sequence of [possibly nested] function calls.
This flow is what you can wrap into exception handlers. Data structures,
objects and their relationships are beside the point.

--
Best regards,
Andrey Tarasevich
Sep 17 '08 #7
Ruben wrote:
On Tue, 16 Sep 2008 19:59:19 -0400, Kai-Uwe Bux wrote:
> Exceptions thrown at
that point cannot be caught by a try-catch block in the constructor since
the constructor is no longer part of the stack.

Yes - I can see that. It makes it seem that throw, try and catch are
very limited tools and I just don't see how it improves error handling.

Other than wrapping main into a try block, it seems hard to get anything
useful out of it in terms of organizing exception handling.
Any even still, an class object called from and in a has relationship
with a class oject I would think would still be in the stack of the
claling object.
You missed something: there is no "stack of the (calling) object".

Also, in C++ object do not call one another. The relation

__ calls __

is a relation between methods/functions, not a relation between objects.

Best

Kai-Uwe Bux
Sep 17 '08 #8
"Ruben" <ru***@www2.mrbrklyn.comwrote in message news:pa****************************@www2.mrbrklyn. com...
On Tue, 16 Sep 2008 12:50:26 -0700, Andrey Tarasevich wrote:
>
Same thing. The search for the matching handler is not in any way tied to
the data structures. It has nothing to do with inheritance or any other
relationships, like 'has-a'. As stated above, it is defined by the
run-time sequence of function call contexts.
but if one object is calling anoother object why is it not in the stack
An object does not call. An object cannot be called.
main calls a construstor which instantates an object which calls another
constructor which instantates another object which causes an error.
The constructor is a function. One function can call another function.
A constructor can call another constructor. In that case the first
constructor can "catch" exceptions "thrown" by the second constructor.
E.g.:

class FirstClass {
AnotherClass AnotherObject;
FirstClass (); // Constructor
};

FirstClass::FirstClass () try
: AnotherObject () // Call construcor of another class
{
} catch (...) {
// Handle exceptions in the construction of AnotherObject.
}
}
Sep 17 '08 #9

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

Similar topics

5
by: Michelle Kinsey-Clinton | last post by:
Hello, I am writing an ASP app which is giving me some very frustrating errors. They appear intermittently, no real pattern to them, and often go away if you reload, or back up a few pages and...
1
by: Kevin Saff | last post by:
I would like to be able to create a std::exception from a user-defined stream, like: error << "Error: " << filename_ << " not found!" << std::endl; I figured I would just create a simple...
3
by: pervinder | last post by:
Hi, I am using STL from http://www.stlport.org/download.html (ver 4.6.2) I see hundered's of error while compilation. I have a c++ prog which uses stl. I am providing the stlport in the include -I...
0
by: Simon Riggs | last post by:
I'm trying to create some User Defined Functions, which act as scalar functions. I can successfully create a UDF like this: drop function arraylistset; CREATE FUNCTION ArrayListSet (ArrayList...
5
by: john.halet | last post by:
This line of code had been working with out issue, now its throwing errors. In my case I have three items in the ComboBox. If I try to change the selected index it throw the error. Earler in...
2
by: Anthony Biondo Jr | last post by:
I was wondering how to handle an error in a web service. If our web service encounters a connection error or any other error what is the best practice for returning an error? Do you return a...
3
by: Baheri | last post by:
Can some some guide me to a good link on how to throw errors in a webservice? Also if there is a standard way of defining different degress of erros while exposing your webservice to thrid parties?...
3
by: CF FAN | last post by:
cf search using verity for my web site. I actually took the code from docs on teh web and tweeked a couple things out on it. Now it is throwing errors and I can't figure out why. What is funny is...
0
by: saapphire | last post by:
here is my code.. private void button2_Click(object sender, EventArgs e) { String str1 = "select * from table1"; dad1 = new...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...
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...

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.