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

Getting the exception object within catch(...)

Hi!

In my main() function I have a last-resort exception construct that
looks
like this:

int main() {
try {
// ... program code
}
catch(...) { // catch every exception
std::cout << "An exception occured!" << std::endl;
};
};

It works fine, but I never know *which* exception has occured.
I seem to remember you can catch an exception object rather than
an exception class, and then, having the precise object you could
get the name that's stored somewhere inside it. But the "..." seems
to get in the way for me. Is there a way to catch a precise exception
object with catch(...)? Or a workaround for that?

tia,
- J.
Jul 22 '05 #1
5 2196
"Jacek Dziedzic" <ja************@janowo.net> wrote in message
news:bq**********@korweta.task.gda.pl...
Hi!

In my main() function I have a last-resort exception construct that
looks
like this:

int main() {
try {
// ... program code
}
catch(...) { // catch every exception
std::cout << "An exception occured!" << std::endl;
};
};

It works fine, but I never know *which* exception has occured.
I seem to remember you can catch an exception object rather than
an exception class, and then, having the precise object you could
get the name that's stored somewhere inside it. But the "..." seems
to get in the way for me. Is there a way to catch a precise exception
object with catch(...)? Or a workaround for that?

tia,
- J.

Sure:

#include <exception> // for std::exception

int main() {
try {
// ... program code
}
catch(const std::exception &e) // catch any standard exception
{
std::cout << e.what() << '\n';
}
catch(...) { // catch any other exception
std::cout << "An exception occured!" << std::endl;
}
};

If you have any exception classes you have written yourself you should
derive them from std::exception so this will work. One tiny point:
std::exception::what is a virtual function which you may override if you
wish. But if you do, make sure you don't declare the catch phrase as:

catch (std::exception e) // bad idea

because this will bit slice down to a plain jane std::exception and your
function won't get called.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #2
"Jacek Dziedzic" <ja************@janowo.net> wrote...
In my main() function I have a last-resort exception construct that
looks
like this:

int main() {
try {
// ... program code
}
catch(...) { // catch every exception
std::cout << "An exception occured!" << std::endl;
};
};

It works fine, but I never know *which* exception has occured.
'catch' with ellipsis will catch them all and it is, as you said,
the last resort. You don't get to know what exception was caught.
I seem to remember you can catch an exception object rather than
an exception class,
You always catch an object.
and then, having the precise object you could
get the name that's stored somewhere inside it. But the "..." seems
to get in the way for me. Is there a way to catch a precise exception
object with catch(...)? Or a workaround for that?


There is no way to know what exception triggered the 'catch_all'
clause unless you re-throw it and catch it again somehow differently:

void thrower()
{
throw 42;
}

#include <iostream>

void foo()
{
try
{
thrower();
}
catch(...)
{
std::cout << "caught something...\n";
throw;
}
}

int main()
{
try
{
foo();
}
catch(int i)
{
std::cout << "caught int(" << i << ")\n";
}
catch(...)
{
std::cout << "caught something again...\n";
}
return 0;
}
Now, if in your 'main' you re-throw the exception you caught, you can
get the environment report it to you. Of course, in that case you will
usually get something like "Uncaught exception blah! Abnormal program
termination!", but since you have caught it already, you get a chance
to close everything that is still open by the time 'catch(...)' is
entered.

Victor
Jul 22 '05 #3

"Jacek Dziedzic" <ja************@janowo.net> wrote in message
news:bq**********@korweta.task.gda.pl...
Hi!

In my main() function I have a last-resort exception construct that
looks
like this:

int main() {
try {
// ... program code
}
catch(...) { // catch every exception
std::cout << "An exception occured!" << std::endl;
};
};

It works fine, but I never know *which* exception has occured.
I seem to remember you can catch an exception object rather than
an exception class, and then, having the precise object you could
get the name that's stored somewhere inside it. But the "..." seems
to get in the way for me. Is there a way to catch a precise exception
object with catch(...)? Or a workaround for that?


Normally, you don't really care about the object. Yes, you need one
technically, but all the information you need is in the type of the class
itself. The object can be totally empty.
class Exception1{};
class Exception 2{};
int main()
{
try
{
throw Exception1();
}
catch(Exception1)
{
std::cout << "Exception1 occurred" << std::endl;
}
catch(Exception2)
{
std::cout << "Exception2 occurred" << std::endl;
}
}
Jul 22 '05 #4
"Cy Edmunds" <ce******@spamless.rochester.rr.com> wrote in message
news:MK********************@twister.nyroc.rr.com.. .
[...]


Thanks a lot!

- J.
Jul 22 '05 #5
On Wed, 3 Dec 2003 22:26:21 +0100, "Jacek Dziedzic" <ja************@janowo.net> wrote:
Hi!

In my main() function I have a last-resort exception construct that
looks
like this:

int main() {
try {
// ... program code
}
catch(...) { // catch every exception
std::cout << "An exception occured!" << std::endl;
};
};

It works fine, but I never know *which* exception has occured.
I seem to remember you can catch an exception object rather than
an exception class, and then, having the precise object you could
get the name that's stored somewhere inside it. But the "..." seems
to get in the way for me. Is there a way to catch a precise exception
object with catch(...)? Or a workaround for that?


Cy Edmunds and others have already answered the real question.

Now here's an answer to the literal question.

In many cases (actually, most cases of real software) you will have to
deal with non-standard exceptions of various kinds from a number of libraries.
Using try-catch-catch-catch everywhere is then redundant and a maintainance
nightmare. So instead you'd like to use catch(...) and _translate_ that
exception, whatever it may be, to a common form.

In standard C++ you can do that by rethrowing the exception and recatching
the exception within the catch(...), where this rethrowing and recatching is
performed by a common exception translation function (which must know about
all the most common kinds of exceptions in the application).

This technique does not work with some older compilers. In particular, a
compiler bug in Visual C++ prevented using this technique with earlier
incantations of that compiler.

Jul 22 '05 #6

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

Similar topics

8
by: andrewpalumbo | last post by:
I'm trying to write some code which will split up a vector into two halves and run a method on the objects in the vector using two seperate threads. I was hoping to see a near linear speedup on an...
3
by: John Ruiz | last post by:
Hello. I was wondering about exceptions and how to throw them within functions/methods and catch them. Suppose that we've got: ----------------------------------------------------------------...
21
by: Michael Bierman | last post by:
Please forgive the simplicy of this question. I have the following code which attempts to determine the color of some text and set other text to match that color. It works fine in Firefox, but does...
44
by: craig | last post by:
I am wondering if there are some best practices for determining a strategy for using try/catch blocks within an application. My current thoughts are: 1. The code the initiates any high-level...
1
by: Noor | last post by:
Hi all, I am trying to catch all types of exceptions from a app regardless of whether it is in debugger mode( VS development environment) or run the.exe file outside the IDE. My App...
8
by: Cybert | last post by:
I just installed Microsoft Visual Basic .NET and wrote a very simple program that references a .tlb file called "Wilbur". The first time I ran the program below everything worked fine. But...
5
by: Simon Tamman {Uchiha Jax} | last post by:
Now this is bugging me. I just released software for a client and they have reported an unhandled stack overflow exception. My first concern is that the entirity of the UI and any threaded...
2
by: karinmorena | last post by:
I'm having 4 errors, I'm very new at this and I would appreciate your input. The error I get is: Week5MortgageGUI.java:151:cannot find symbol symbol: method allInterest(double,double,double)...
9
by: TC | last post by:
Hey All, I posted this to the Crypto users group and forgot to add the VB.Net users group. I apologize for any confusion. I have been testing a try / catch / finally block and purposely...
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: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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...

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.