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

catch(...) doesn't catch everything

I was looking for a way to just catch any exception throw. Given the
postings, it looks like catch(...) should do it, except it doesn't.
Maybe C++ is suppose to work this way, or maybe this is a bug in g++.

I have a small program to demonstrate below. I compiled on a RH9
system using g++ v3.4.5.

#include <iostream>

main()
{
try {
throw(1);
} catch (...) {
std::cerr << "Caught integer exception\n";
}

try {
throw;
} catch (...) {
std::cerr << "Caught exception\n";
}
}

The first exception is caught when an integer is thrown. The second
one isn't caught.

I just need to capture an exception because of a possible error in a
constructor (so I can't pass back an error code). I don't need to send
any information in the exception, so I was just using throw without any
arguments. Looks like I'll need to send something just so it works
unless I'm doing something wrong.

Feb 1 '06 #1
9 2462
Adam wrote:
I was looking for a way to just catch any exception throw. Given the
postings, it looks like catch(...) should do it, except it doesn't.
Maybe C++ is suppose to work this way, or maybe this is a bug in g++.

I have a small program to demonstrate below. I compiled on a RH9
system using g++ v3.4.5.

#include <iostream>

main()
{
try {
throw(1);
} catch (...) {
std::cerr << "Caught integer exception\n";
}

try {
throw;
} catch (...) {
std::cerr << "Caught exception\n";
}
}

The first exception is caught when an integer is thrown. The second
one isn't caught.

I just need to capture an exception because of a possible error in a
constructor (so I can't pass back an error code). I don't need to send
any information in the exception, so I was just using throw without any
arguments. Looks like I'll need to send something just so it works
unless I'm doing something wrong.


to be honest, I'm surprised that the second one even compiled. The
argumentless version of throw is for use inside a catch clause, to
rethrow the caught exception.

Feb 1 '06 #2
Adam wrote:
I was looking for a way to just catch any exception throw. Given the
postings, it looks like catch(...) should do it, except it doesn't.
Maybe C++ is suppose to work this way, or maybe this is a bug in g++.

I have a small program to demonstrate below. I compiled on a RH9
system using g++ v3.4.5.

#include <iostream>

main()
{
try {
throw(1);
} catch (...) {
std::cerr << "Caught integer exception\n";
}

try {
throw;
} catch (...) {
std::cerr << "Caught exception\n";
}
}


Actually, on further review, your code is behaving properly.

Standard, 15.1/8 "If no exception is presently being handled, executing
a throw-expression with no operand calls terminate(). Since your second
throw is not in an exception handler (catch clause), your program calls
terminate(). Go to terminate(), go directly to terminate(), do not pass
GO, do not collect $200.

Feb 1 '06 #3
red floyd wrote:
Adam wrote:
I was looking for a way to just catch any exception throw. Given the
postings, it looks like catch(...) should do it, except it doesn't.
Maybe C++ is suppose to work this way, or maybe this is a bug in g++.

I have a small program to demonstrate below. I compiled on a RH9
system using g++ v3.4.5.

#include <iostream>

main()
{
try {
throw(1);
} catch (...) {
std::cerr << "Caught integer exception\n";
}

try {
throw;
} catch (...) {
std::cerr << "Caught exception\n";
}
}


Actually, on further review, your code is behaving properly.

Standard, 15.1/8 "If no exception is presently being handled, executing
a throw-expression with no operand calls terminate(). Since your second
throw is not in an exception handler (catch clause), your program calls
terminate(). Go to terminate(), go directly to terminate(), do not pass
GO, do not collect $200.


Damn. Missed a close quote. The quote from the Standard ends before
the word "Since". So the text should read '... operand calls
terminate()."' Everything from "Since your second..." is my commentary.

Feb 1 '06 #4
Adam <ad**@scan.mc.xerox.com> wrote:
I was looking for a way to just catch any exception throw. Given the
postings, it looks like catch(...) should do it, except it doesn't.
Maybe C++ is suppose to work this way, or maybe this is a bug in g++.

I have a small program to demonstrate below. I compiled on a RH9
system using g++ v3.4.5.

#include <iostream>

main()
{
try {
throw(1);
} catch (...) {
std::cerr << "Caught integer exception\n";
}

try {
throw;
} catch (...) {
std::cerr << "Caught exception\n";
}
}

The first exception is caught when an integer is thrown. The second
one isn't caught.

I just need to capture an exception because of a possible error in a
constructor (so I can't pass back an error code). I don't need to send
any information in the exception, so I was just using throw without any
arguments. Looks like I'll need to send something just so it works
unless I'm doing something wrong.


IIRC, throw with no arguments will simply re-throw the current
exception; it is usually used in cases that need to perform local
cleanup before propagating the exception further up the stack. In the
second case, there is no current exception, so nothing is thrown.

--
Marcus Kwok
Feb 1 '06 #5
Marcus Kwok wrote:
IIRC, throw with no arguments will simply re-throw the current
exception; it is usually used in cases that need to perform local
cleanup before propagating the exception further up the stack. In the
second case, there is no current exception, so nothing is thrown.


In the second case, terminate() is called (15.1/8).
Feb 2 '06 #6
>> main()
{
try {
throw;
} catch (...) {
std::cerr << "Caught exception\n";
}
}
to be honest, I'm surprised that the second one even compiled. The
argumentless version of throw is for use inside a catch clause, to rethrow
the caught exception.


Yes, but the question of whether "throw;" is being invoked in the context of
a catch block can not be generally decided in the compile time. Consider the
following example:

void f()
{
throw; // (1)
}

void g()
{
try { throw 1; } catch ( ... ) { f(); } // (2)
}

When the compiler generates code for (1), it can not issue a diagnostic
because "throw;" is perfectly valid when it is called from a catch block, as
in (2). Therefore, the actual behaviour depends on the current execution
path of the program and is determined in the run time.

Regards
Marek


Feb 2 '06 #7
>> Standard, 15.1/8 "If no exception is presently being handled, executing
a throw-expression with no operand calls terminate(). Since your second
throw is not in an exception handler (catch clause), your program calls
terminate(). Go to terminate(), go directly to terminate(), do not pass
GO, do not collect $200.


Damn. Missed a close quote. The quote from the Standard ends before
the word "Since". So the text should read '... operand calls
terminate()."' Everything from "Since your second..." is my commentary.


Thanks for the information. Now I understand why the code acts like it
does. Guess I'll just have to give an argument to throw to catch it.

Feb 2 '06 #8
red floyd <no*****@here.dude> wrote:
Marcus Kwok wrote:

IIRC, throw with no arguments will simply re-throw the current
exception; it is usually used in cases that need to perform local
cleanup before propagating the exception further up the stack. In the
second case, there is no current exception, so nothing is thrown.

In the second case, terminate() is called (15.1/8).


Thanks, I saw it in your other post after I had already posted mine.

--
Marcus Kwok
Feb 2 '06 #9
Marek Vondrak wrote:
main()
{
try {
throw;
} catch (...) {
std::cerr << "Caught exception\n";
}
}

to be honest, I'm surprised that the second one even compiled. The
argumentless version of throw is for use inside a catch clause, to rethrow
the caught exception.


Yes, but the question of whether "throw;" is being invoked in the context of
a catch block can not be generally decided in the compile time. Consider the
following example:

void f()
{
throw; // (1)
}

void g()
{
try { throw 1; } catch ( ... ) { f(); } // (2)
}

When the compiler generates code for (1), it can not issue a diagnostic
because "throw;" is perfectly valid when it is called from a catch block, as
in (2). Therefore, the actual behaviour depends on the current execution
path of the program and is determined in the run time.

Good point. "throw;" not in a catch block, should compile. I guess
that's what 15.1/8 is for.
Feb 2 '06 #10

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

Similar topics

20
by: Tom Groszko | last post by:
Given a try catch scenario try { dosomething(); } catch (...) { report some error } Is there any way to figure out what exception is being thrown and
13
by: Benny | last post by:
Hi, I have something like this: try { // some code } catch // note - i am catching everything now {
2
by: Sam Miller | last post by:
Normally the debugger (visual studio .net environment) is good at pointing out the line of code that caused an exception.... except when that code is executed as part of a timer handler. In the...
2
by: sbalak | last post by:
I am writing code in C# and I wanted a basic solution: In Try / Catch block, I want to get a general Exception. So, I write code as: try { // do something } catch (SomethingException ex) {
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...
7
by: Mr Flibble | last post by:
Are try { //something } catch { // }
23
by: pigeonrandle | last post by:
Hi, Does this bit of code represent complete overkill?! try { //create a treenode TreeNode tn = new TreeNode(); //add it to a treeview tv.Nodes.Add(tn);
7
by: dick | last post by:
in the "try{throw}catch" structure, how the C++ code return the "type" thrown by a function?
0
by: glubber | last post by:
I have a cronned perl script that runs on a different server. when I try to restore it to the server below things dont copy across correctly. the target server is described below. debian 4.0...
5
by: vmnvmcxbv | last post by:
Why does this: try { throw 1; } catch(...) { handle_exception(); }
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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
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,...
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
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...

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.