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. 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.
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.
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.
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
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).
>> 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
>> 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.
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
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. This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
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
|
by: Benny |
last post by:
Hi,
I have something like this:
try
{
// some code
}
catch // note - i am catching everything now
{
|
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...
|
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)
{
|
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...
|
by: Mr Flibble |
last post by:
Are
try
{
//something
}
catch
{
//
}
|
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);
|
by: dick |
last post by:
in the "try{throw}catch" structure, how the C++ code return the "type"
thrown by a function?
|
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...
|
by: vmnvmcxbv |
last post by:
Why does this:
try
{
throw 1;
}
catch(...)
{
handle_exception();
}
|
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...
|
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
|
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...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
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: 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,...
|
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,...
|
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...
|
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...
| |