default exception-handling

V

Vladimir Ciobanu

Ekim said:
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
 
V

Vladimir Ciobanu

[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
 
M

Moritz Beller

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
 
V

Vladimir Ciobanu

[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
 
E

Ekim

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
 
E

Ekim

Vladimir Ciobanu said:
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.


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())?
 
E

Ekim

Vladimir Ciobanu said:
[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!
 
J

Jerry Coffin

Ekim said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,014
Latest member
BiancaFix3

Latest Threads

Top