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

Missed catch?

I have a throw as follows:
void bar() {
ostringstream o;
o << foo;
throw o.str();
}
I used to catch it is follows:
void baz() {
try {
bar();
} catch (string s) {
cerr << "CAUGHT";
}
}

That worked just fine. But when I moved the 'try ... catch' block to
main() (which calls baz()), the exception is NOT caught. I just get a
terminate.
Why is that? How can I catch the exception in main?

Thanks,
Joseph

Mar 29 '06 #1
7 1703
Joseph Turian wrote:
I have a throw as follows:
void bar() {
ostringstream o;
o << foo;
throw o.str();
}
I used to catch it is follows:
void baz() {
try {
bar();
} catch (string s) {
cerr << "CAUGHT";
}
}

That worked just fine. But when I moved the 'try ... catch' block to
main() (which calls baz()), the exception is NOT caught. I just get a
terminate.
Why is that? How can I catch the exception in main?

Thanks,
Joseph


First of all, you should not throw an object (such as std::string)
whose copy constructor might also throw. That's a recipe for disaster
and possibly the cause of your woes here. See
http://www.boost.org/more/error_handling.html for some excellent advice
on how to use exceptions properly.

Second, you should catch by (const) reference. See this FAQ and the
following one:

http://www.parashift.com/c++-faq-lit....html#faq-17.6

Third, there is no issue with having the code above in main() instead
of baz(), except for those issues already mentioned. Show us a
complete, compilable example that demonstrates your problem, and we
might be able to help further.

Cheers! --M

Mar 29 '06 #2
I read the documents you cited and tried the changes you proposed.
However, no luck.

Here's actual excerpts from the code:

class l1_exception : public exception {
};

....

l1_exception l1ex;
void parameter::set_l1_penalty_factor(double d) {
...
if (parameter::l1_penalty_factor() <
parameter::final_l1_penalty_factor()) {
throw l1ex;
}
...
}

// The following catch is successful
try {
parameter::set_l1_penalty_factor(max_gain() *
parameter::l1_penalty_factor_overscale());
} catch (l1_exception& e) {
exit(0);
}

// If we comment out the above catch, the following catch in main()
misses:
try {
train(type, example_file);
} catch (l1_exception& e) {
exit(0);
}

I get the following terminate() message:
terminate called after throwing an instance of 'l1_exception'
what(): 12l1_exception
How can I correct the catch in main()?

Thanks,

Joseph

Mar 29 '06 #3
I get the following terminate() message:
terminate called after throwing an instance of 'l1_exception'
what(): 12l1_exception

hmmm... so if I compile *without* -fomit-frame-pointer, I catch the
exception just fine.
Why is this?

Joseph

Mar 29 '06 #4
MLimber wrote:
Show us a complete, compilable example that demonstrates
your problem, and we might be able to help further.

Joseph Turian wrote:
Here's actual excerpts from the code:


[snip]

Joe -

If you want anyone to help you, you're going to have to comply with
MLimber's request. What you posted is not a "complete compilable
example that demonstrates your problem." Please help us help you.

Best regards,

Tom

Mar 29 '06 #5
Joseph Turian wrote:
I get the following terminate() message:
terminate called after throwing an instance of 'l1_exception'
what(): 12l1_exception

hmmm... so if I compile *without* -fomit-frame-pointer, I catch the
exception just fine.
Why is this?

Joseph


A frame is required for exception handling to work across function
calls.

Larry
Mar 30 '06 #6

Larry I Smith wrote:
Joseph Turian wrote:

hmmm... so if I compile *without* -fomit-frame-pointer, I catch the
exception just fine.
Why is this?

Joseph


A frame is required for exception handling to work across function
calls.

Larry


I'm not sure I follow you. Are you suggesting that
-fomit-frame-pointer prevents exception handling across function calls?
It seems to work for me.

Best regards,

Tom

Mar 30 '06 #7
Thomas Tutone wrote:
Larry I Smith wrote:
Joseph Turian wrote:

hmmm... so if I compile *without* -fomit-frame-pointer, I catch the
exception just fine.
Why is this?

Joseph

A frame is required for exception handling to work across function
calls.

Larry


I'm not sure I follow you. Are you suggesting that
-fomit-frame-pointer prevents exception handling across function calls?
It seems to work for me.

Best regards,

Tom


Perhaps it is hardware/OS related?

This snip from the GCC 'info' pages seems to suggest
the frame and exceptions are inter-related on some platforms.

`-fexceptions'
Enable exception handling. Generates extra code needed to
propagate exceptions. For some targets, this implies GCC will
generate frame unwind information for all functions, which can
produce significant data size overhead, although it does not
affect execution. If you do not specify this option, GCC will
enable it by default for languages like C++ which normally require
exception handling, and disable it for languages like C that do
not normally require it. However, you may need to enable this
option when compiling C code that needs to interoperate properly
with exception handlers written in C++. You may also wish to
disable this option if you are compiling older C++ programs that
don't use exception handling.

The OP's question should probably be directed to the newsgroup:

gnu.g++.help

Regards,
Larry
Mar 30 '06 #8

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

Similar topics

10
by: Gary.Hu | last post by:
I was trying to catch the Arithmetic exception, unsuccessfully. try{ int a = 0, b = 9; b = b / a; }catch(...){ cout << "arithmetic exception was catched!" << endl; } After ran the program,...
11
by: kaeli | last post by:
Hey all, I'd like to start using the try/catch construct in some scripts. Older browsers don't support this. What's the best way to test for support for this construct so it doesn't kill...
4
by: Abhishek Srivastava | last post by:
Hello All, I have seen code snippets like try { ..... } catch {
11
by: Pohihihi | last post by:
I was wondering what is the ill effect of using try catch in the code, both nested and simple big one. e.g. try { \\ whole app code goes here } catch (Exception ee) {}
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
23
by: VB Programmer | last post by:
Variable scope doesn't make sense to me when it comes to Try Catch Finally. Example: In order to close/dispose a db connection you have to dim the connection outside of the Try Catch Finally...
32
by: cj | last post by:
Another wish of mine. I wish there was a way in the Try Catch structure to say if there wasn't an error to do something. Like an else statement. Try Catch Else Finally. Also because I...
2
by: david | last post by:
I wrote one function which is used to count comment and code lines and it works as it should be, but when compiling I get warning (i686-apple- darwin9-g++-4.0.1): pirma.cpp: In function ‘int...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
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...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
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...

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.