Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > default exception-handling

Reply
Thread Tools

default exception-handling

 
 
Vladimir Ciobanu
Guest
Posts: n/a
 
      08-30-2004
"Ekim" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
> 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


 
Reply With Quote
 
 
 
 
Vladimir Ciobanu
Guest
Posts: n/a
 
      08-30-2004
"Ekim" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
[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


 
Reply With Quote
 
 
 
 
Moritz Beller
Guest
Posts: n/a
 
      08-30-2004
On Mon, 30 Aug 2004 12:28:52 +0300
"Vladimir Ciobanu" <(E-Mail Removed)> 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
 
Reply With Quote
 
Vladimir Ciobanu
Guest
Posts: n/a
 
      08-30-2004
"Ekim" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)...
[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


 
Reply With Quote
 
Ekim
Guest
Posts: n/a
 
      08-30-2004
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


 
Reply With Quote
 
Ekim
Guest
Posts: n/a
 
      08-30-2004

"Vladimir Ciobanu" <(E-Mail Removed)> wrote in message
news:cgus2h$d1h$(E-Mail Removed)...
> "Ekim" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> > 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())?


 
Reply With Quote
 
Ekim
Guest
Posts: n/a
 
      08-30-2004
"Vladimir Ciobanu" <(E-Mail Removed)> wrote in message
news:cgv03i$fj9$(E-Mail Removed)...
> "Ekim" <(E-Mail Removed)> wrote in message
> news:(E-Mail Removed)...
> [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!


 
Reply With Quote
 
Jerry Coffin
Guest
Posts: n/a
 
      08-30-2004
"Ekim" <(E-Mail Removed)> wrote in message news:<(E-Mail Removed)>...
> 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.
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Why is Default.aspx not present in the Default Content page? Gaetan ASP .Net 3 02-14-2006 07:24 PM
default.mspx or default.do micky ASP .Net 1 12-17-2005 12:42 AM
Default Domain Policy vs Default Domain Controller Policy Tyler Cobb MCSE 6 10-19-2005 09:36 PM
Default Mozilla isn't default! Help! M. FERRANTE Firefox 1 07-27-2005 09:07 AM
automatic default route propagation into RIP: default metric Ben Low Cisco 2 12-28-2003 03:57 AM



Advertisments