By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,889 Members | 1,358 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,889 IT Pros & Developers. It's quick & easy.

default exception-handling

P: n/a
hy,

I've a question concerning exception-handling in c++:
is there a possibility to catch any exception (I know one can do that by
"catch(...)") and display its default-error-message (like in Java)?
or do you have to create your own exceptions and make your own
error-messages?

I would only need the error-number or something like a short description
which exception occured, and subsequently I want to write it to a logfile.
Therefore I want to log every exception, and not a specific exception I had
to define beforehand.

hope you understand my problem,
thx in advance,

ekim
Jul 22 '05 #1
Share this Question
Share on Google+
7 Replies


P: n/a
"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
hy,

I've a question concerning exception-handling in c++:
is there a possibility to catch any exception (I know one can do
that by
"catch(...)") and display its default-error-message (like in Java)?
or do you have to create your own exceptions and make your own
error-messages?
No, there isn't. You can only catch an exception if you know its type,
or you
can use the "catch(...)" form. Exceptions are made to postpone error
handling
up to a point where there's enough information to do something
meaningful.
I would only need the error-number or something like a short
description
which exception occured, and subsequently I want to write it to a
logfile.
Therefore I want to log every exception, and not a specific
exception I had
to define beforehand.


Ok, I'll admit. If the exceptions have a common base class, you can
catch them
by a reference to that class (and all the standard exceptions derive
from
std::exception). However, note that if there are user-defined
exceptions that don't
have std::exceptions as a base class, your catch( std::exception& e )
won't match.

Vladimir Ciobanu
Jul 22 '05 #2

P: n/a

"Vladimir Ciobanu" <rh***@rdsnet.ro> wrote in message
news:cg**********@domitilla.aioe.org...
"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
hy,

I've a question concerning exception-handling in c++:
is there a possibility to catch any exception (I know one can do
that by
"catch(...)") and display its default-error-message (like in Java)?
or do you have to create your own exceptions and make your own
error-messages?


No, there isn't. You can only catch an exception if you know its type,
or you
can use the "catch(...)" form. Exceptions are made to postpone error
handling
up to a point where there's enough information to do something
meaningful.
I would only need the error-number or something like a short
description
which exception occured, and subsequently I want to write it to a
logfile.
Therefore I want to log every exception, and not a specific
exception I had
to define beforehand.


Ok, I'll admit. If the exceptions have a common base class, you can
catch them
by a reference to that class (and all the standard exceptions derive
from
std::exception). However, note that if there are user-defined
exceptions that don't
have std::exceptions as a base class, your catch( std::exception& e )
won't match.

Vladimir Ciobanu


hy vladimir,
thx for your immediate response. so if I got you right, I could catch
std::exception and put the error-messages into my logfile like this:

catch (std::exception& e)
{
cout << "Exception: " << e.what();
}

is this std::exception thrown for all "normal" errors like bad memory
allocation, wrong filename when opening a file and so on? or which
exceptions does this one cover?

what's more, is it enough to handle that let's say in the main-function, and
what do I have to specify in all the other methods in order to make them
throw any appearing exception (something like : throw())?
Jul 22 '05 #3

P: n/a
"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
[snip]
so if I got you right, I could catch
std::exception and put the error-messages into my logfile like this:

catch (std::exception& e)
{
cout << "Exception: " << e.what();
}
Yes. But you will lose the additional information the type would give
you.
However, if you are just trying to log the error message and re-throw
it,
this is fine as long as you realize the requirement is that every
exception
must inherit from std::exception. See below for more details.

is this std::exception thrown for all "normal" errors like bad
memory
allocation, wrong filename when opening a file and so on? or which
exceptions does this one cover?
In the C++ standard all the exceptions listed under 19.1[3] derive
from
std::exception. These are:

logic_error, domain_error, invalid_argument; length_error,
out_of_range,
runtime_error, range_error, overflow_error, underflow_error and
bad_alloc (18.4.2.1) - of course, all in the std namespace.

To my knowledge, these is a complete list of exceptions in the C++
standard.
As I said in the previous post, this will only catch exceptions that
are derived
from std::exception (and std::exception itself, of course).
what's more, is it enough to handle that let's say in the
main-function, and
what do I have to specify in all the other methods in order to make
them
throw any appearing exception (something like : throw())?


If by that you mean "if I catch an exception at some point, but decide
not to handle
it, how do I re-throw it, you just use the throw expression without an
operand
( just: throw; ) as stated in 15.1[6].

Vladimir Ciobanu
Jul 22 '05 #4

P: n/a
"Vladimir Ciobanu" <rh***@rdsnet.ro> wrote in message
news:cg**********@domitilla.aioe.org...
"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
[snip]
so if I got you right, I could catch
std::exception and put the error-messages into my logfile like this:

catch (std::exception& e)
{
cout << "Exception: " << e.what();
}


Yes. But you will lose the additional information the type would give
you.
However, if you are just trying to log the error message and re-throw
it,
this is fine as long as you realize the requirement is that every
exception
must inherit from std::exception. See below for more details.

is this std::exception thrown for all "normal" errors like bad
memory
allocation, wrong filename when opening a file and so on? or which
exceptions does this one cover?


In the C++ standard all the exceptions listed under 19.1[3] derive
from
std::exception. These are:

logic_error, domain_error, invalid_argument; length_error,
out_of_range,
runtime_error, range_error, overflow_error, underflow_error and
bad_alloc (18.4.2.1) - of course, all in the std namespace.

To my knowledge, these is a complete list of exceptions in the C++
standard.
As I said in the previous post, this will only catch exceptions that
are derived
from std::exception (and std::exception itself, of course).
what's more, is it enough to handle that let's say in the
main-function, and
what do I have to specify in all the other methods in order to make
them
throw any appearing exception (something like : throw())?


If by that you mean "if I catch an exception at some point, but decide
not to handle
it, how do I re-throw it, you just use the throw expression without an
operand
( just: throw; ) as stated in 15.1[6].

Vladimir Ciobanu


thanks a lot so far - I understand quite a bit more of exceptions now. howev
er, I do not understand why I still get an error-message when executing the
following program:

extern "C" BOOL WINAPI DllMain(HINSTANCE hInstance, DWORD dwReason, LPVOID
lpReserved) // normal dll-main-function
{
try
{
FILE *stream;
stream = fopen( "ThisFileDoesNotExistnot.txt", "r" ); // try
to force an error (open file for reading that does not exist)
fprintf(stream, "%s\n", "trying to produce an error");
fclose(stream);

switch(dwReason)
// normal dll-main-function
{
case DLL_PROCESS_ATTACH:
g_hInst = hInstance;
break;
case DLL_PROCESS_DETACH:
break;
}
}
catch(...)
// catch any exception
{
FILE *stream;
stream = fopen( "MyException.txt", "a" );
fprintf(stream, "%s\n", "DllMain - general exception(...) occured");
// write exception-info to logfile
fclose(stream);
}
return TRUE;
}
As explained a little in the comments, I try to force an error at will by
opening a file for reading that does not exist. In the catch-block I catch
any exception with catch(...) and in there I just write an error-message
into a logfile. Nothingtheless, at execution time an error-window pops up
with a message like "debug assertion failed!" (the error is definitely from
the fopen-statement as I intended).

Why does this error-message still pop up although I try to catch all
possible exceptions? My topmost aim is to avoid queer errors and sudden
program terminations like this - instead, I want to have a message written
into my logfile and terminate the program in a normal way.

Did I make something wrong at catching (...) or what's wrong at all with my
code?

thx again in advance,
ekim!
Jul 22 '05 #5

P: n/a
On Mon, 30 Aug 2004 12:28:52 +0300
"Vladimir Ciobanu" <rh***@rdsnet.ro> wrote:
I would only need the error-number or something like a short
description
which exception occured, and subsequently I want to write it to a
logfile.
Therefore I want to log every exception, and not a specific
exception I had
to define beforehand.


Ok, I'll admit. If the exceptions have a common base class, you can
catch them
by a reference to that class (and all the standard exceptions derive
from
std::exception).


And make sure you've noticed it's a REFERENCE to std::exception.
Otherwise you'll get the object slicing problem due to the
pass-by-value-mechanism, which results in converting a derived class
type into the base one (by calling std::exception's copy constructor),
with all consequences such as losing new class members.

best regards / Gruß
Moritz Beller
--
web http://www.4momo.de
mail momo dot beller at t-online dot de
gpgkey http://gpg.notlong.com
Jul 22 '05 #6

P: n/a
"Ekim" <th************@gmx.net> wrote in message
news:2p************@uni-berlin.de...
[snip]
stream = fopen( "ThisFileDoesNotExistnot.txt", "r" );
// try
// to force an error (open file for reading that does not exist)
fopen / fclose / fprintf / etc functions are "inherited" by C++ from
the C
library. As C doesn't have exceptions, these functions will not throw
in case of error. Instead, compiled in debuging mode, they produce a
runtime assertion error.
Not even C++ IOstreams (ifstream / ofstream) do not throw exceptions.
Streams have fail bits which can be checked for errors.
catch(...)
// catch any exception
{
FILE *stream;
stream = fopen( "MyException.txt", "a" );
fprintf(stream, "%s\n", "DllMain - general exception(...)
occured");
// write exception-info to logfile
fclose(stream);
}
return TRUE;
}
As I said, there's nothing to catch. There's no exception thrown -
it's just a
runtime assertion that causes the program to stop from executing.
My topmost aim is to avoid queer errors and sudden
program terminations like this - instead, I want to have a message
written
into my logfile and terminate the program in a normal way.


One way would be checking the stream's fail bits and throw an
exception.
Consider you need to read a settings file that provides you with the
default
font size, color, etc. You would have a class that would open a file
stream,
reads the settings and saves them for later use. If any of the
previous operations
fail (file doesn't exist, file doesn't have the correct syntax,
settings missing or
have inconsistent values, not enough memory to store the settings,
etc), your
class would throw an exception.
If you do all that in the constructor, you would do what is known as
RAII
(Resource Acquisition Is Initialization). You can probably google the
term for
a better understanding.

Vladimir Ciobanu
Jul 22 '05 #7

P: n/a
"Ekim" <th************@gmx.net> wrote in message news:<2p************@uni-berlin.de>...
hy,

I've a question concerning exception-handling in c++:
is there a possibility to catch any exception (I know one can do that by
"catch(...)") and display its default-error-message (like in Java)?
or do you have to create your own exceptions and make your own
error-messages?


No and no.

A catch-all clause doesn't let you do much of anything with what you
catch, largely because there may not be anything to do -- it may not
have a default message, or much of anything else.

The usual advice, however, is to derive exception classes from those
in the standard library, and all of these DO have at an associated
string.

As such, what you probably want to do is something like catching the
library's base class, and printing out a diagnostic if you do, and
follow that by a catch-all to catch anything else, realizing that you
won't be able to do much with it.

--
Later,
Jerry.

The universe is a figment of its own imagination.
Jul 22 '05 #8

This discussion thread is closed

Replies have been disabled for this discussion.