default exception-handling

Discussion in 'C++' started by Vladimir Ciobanu, Aug 30, 2004.

  1. "Ekim" <> wrote in message
    news:...
    > 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
    Vladimir Ciobanu, Aug 30, 2004
    #1
    1. Advertising

  2. "Ekim" <> wrote in message
    news:...
    [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
    Vladimir Ciobanu, Aug 30, 2004
    #2
    1. Advertising

  3. On Mon, 30 Aug 2004 12:28:52 +0300
    "Vladimir Ciobanu" <> 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
    Moritz Beller, Aug 30, 2004
    #3
  4. "Ekim" <> wrote in message
    news:...
    [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
    Vladimir Ciobanu, Aug 30, 2004
    #4
  5. Vladimir Ciobanu

    Ekim Guest

    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
    Ekim, Aug 30, 2004
    #5
  6. Vladimir Ciobanu

    Ekim Guest

    "Vladimir Ciobanu" <> wrote in message
    news:cgus2h$d1h$...
    > "Ekim" <> wrote in message
    > news:...
    > > 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())?
    Ekim, Aug 30, 2004
    #6
  7. Vladimir Ciobanu

    Ekim Guest

    "Vladimir Ciobanu" <> wrote in message
    news:cgv03i$fj9$...
    > "Ekim" <> wrote in message
    > news:...
    > [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!
    Ekim, Aug 30, 2004
    #7
  8. Vladimir Ciobanu

    Jerry Coffin Guest

    "Ekim" <> wrote in message news:<>...
    > 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.
    Jerry Coffin, Aug 30, 2004
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    398
  2. Mark Tarver
    Replies:
    22
    Views:
    1,274
    J Kenneth King
    Apr 26, 2009
  3. Peter
    Replies:
    34
    Views:
    1,911
    James Kanze
    Oct 17, 2009
  4. VSK
    Replies:
    0
    Views:
    237
  5. Iñaki Baz Castillo
    Replies:
    1
    Views:
    174
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...

Share This Page