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

uncaught throw.. (exception)

I have a constructor that is being called by a global application
variable.
That constructor is throwing an exception.
As I am not in main() at that point I have no corresponding catch for
that exception.

Ok so what do I do?

I could modify the constructor not to throw an exception...

What else could be done?

Can a method know if there is anyone around to catch the exception
that is about to be thrown?
Or is this blind faith?

Aug 8 '07 #1
3 1893
On Aug 8, 3:50 am, SpreadTooThin <bjobrie...@gmail.comwrote:
I have a constructor that is being called by a global application
variable.
That constructor is throwing an exception.
As I am not in main() at that point I have no corresponding catch for
that exception.

Ok so what do I do?
Where you currently have this:

SomeClass gGlobal;
void foo()
{
gGlobal->use();
}
int main()
{
foo();
}

try this:

extern SomeClass& gGlobal();
void foo()
{
gGlobal()->use(); /* nb parens */
}
SomeClass& gGlobal()
{
static SomeClass sGlobal;
return sGlobal;
}
int main()
{
try
{
foo();
}
catch(...)
{
// whatever
}
}

I'm not sure that this is a guaranteed solution as I think
an implementation is still allowed to instantiate sGlobal before
main() is entered, in which case you're back to square one,
but perhaps someone more knowledgeable can adjudicate. Similarly,
I'm not sure what's guaranteed in a multi-threaded environment.


Aug 8 '07 #2
On Aug 8, 1:24 pm, tragomaskhalos <dave.du.verg...@logicacmg.com>
wrote:
>
Where you currently have this:
[code snipped]
Duh, for "->" read ".".


Aug 8 '07 #3

SpreadTooThin <bj********@gmail.comwrote in message...
I have a constructor that is being called by a global application
variable.
That constructor is throwing an exception.
As I am not in main() at that point I have no corresponding catch for
that exception.
Put the 'try-catch' inside the global object?
Put the global object inside something (with 'try-catch')?
Don't use globals.
>
Ok so what do I do?
I could modify the constructor not to throw an exception...
Or put an 'function level try block' on the constructor.

class Bob{ public:
Bob( size_t const sz ) try : biggy( new int[ sz ] ) {
// if it got here, 'biggy' was probably allocated
}
catch( std::bad_alloc const &){
// handle it
std::exit(0);
}
catch( ThrownType const & ) {
// handle it
// throw SomethingElse("Bob puked!"); // but, same problem.
std::terminate();
}
int *biggy;
};

What else could be done?
Depends on what you *are* trying to do. I can't see your code from here.
>
Can a method know if there is anyone around to catch the exception
that is about to be thrown?
Or is this blind faith?
No, it's 'catch 22'.

--
Bob R
POVrookie
Aug 9 '07 #4

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

Similar topics

4
by: nrhayyal | last post by:
hi all, i am facing a problem in catching an exception which is uncaught in any of the catch block. not doing so will gives me coredump. I tried by rewriting set_unexpected() and set_terminate()...
7
by: meaneyedcat | last post by:
When I call addField(), my table appears to be populated correctly with a new row in the table with all the required fields. However, when I call delete row on any new rows that have been created,...
3
by: c.prerovsky | last post by:
Hi there, I started messing around with JavaScript OOP a few days ago and still can't get this one to work. There are many things wich keep confusing me, eg. the various ways to define a class...
0
by: maweb | last post by:
Hello i'm becoming mad at trapping this exception: EventType clr20r3, P1 execwinservice.exe, P2 1.0.0.0, P3 45eec745, P4 system.data, P5 2.0.0.0, P6 4333aea2, P7 2323, P8 11, P9...
2
Plater
by: Plater | last post by:
I am using the XMLHttpRequest to send a request every 5ish seconds or so. Everything works fine until I take the server down that the object is trying to retrieve data from. Then the firefox...
3
by: George2 | last post by:
Hello everyone, Just want to check whether my understanding is correct, Both (1) and (2) only covers Windows C++ platform. 1. If there is uncaught exception, destructor is not ensured to...
3
by: friend | last post by:
Error: uncaught exception: " nsresult: "0x80004005 (NS_ERROR_FAILURE)" location: "JS frame :: http://manimekalai/VulnMgmt/scanfi/crs_source/vulnupdate/latest.php?vulnerability=2451 ::...
1
by: siddhu | last post by:
Dear experts, Is it a standard behavior that set_terminate handler function gets called if an exception is thrown and not caught anywhere in the program? I know its a C++ forum and I'm facing a...
5
by: Alexandru Mosoi | last post by:
how can I catch (globally) exception that were not caught in a try/ catch block in any running thread? i had this weird case that an exception was raised in one thread, but nothing was...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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
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...
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,...
0
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...

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.