C++ Exceptions

Discussion in 'C++' started by praviarun@gmail.com, Jan 11, 2007.

  1. Guest

    I found that the following code catches exception on gcc 4.0
    i.e it prints "caught standard exception"


    try{
    vector<int> test;
    test.at(0); // this line throws an exception

    }
    catch(...)
    {
    printf("caught standard exception");

    }

    but the following code which dereferences a null pointer does not throw
    an exception

    try{
    int *p = 0;
    *p =10;

    }
    catch(...)
    {

    printf("caught dereferencing null pointer");

    }

    This code simply terminates without catching an exception
    I am using Xcode

    Please let me know how i could catch this exception


    Praveen
    , Jan 11, 2007
    #1
    1. Advertising

  2. Pete Becker Guest

    wrote:
    >
    > Please let me know how i could catch this exception
    >


    In standard C++, if you don't throw it, you can't catch it. Hardware
    "exceptions" are not the same thing as C++ "exceptions".

    --

    -- Pete
    Roundhouse Consulting, Ltd. (www.versatilecoding.com)
    Author of "The Standard C++ Library Extensions: a Tutorial and
    Reference." (www.petebecker.com/tr1book)
    Pete Becker, Jan 11, 2007
    #2
    1. Advertising

  3. Dizzy Guest

    wrote:

    > but the following code which dereferences a null pointer does not throw
    > an exception
    >
    > try{
    > int *p = 0;
    > *p =10;
    >
    > }
    > catch(...)
    > {
    >
    > printf("caught dereferencing null pointer");
    >
    > }


    Of course it does not.

    >
    > This code simply terminates without catching an exception
    > I am using Xcode


    Where does in the C++ standard text says that dereferencinging an invalid
    pointer (null included) should throw a C++ exception ?

    > Please let me know how i could catch this exception


    There is no C++ exception thrown, thus there is no C++ exception to catch.
    If you do not want to have such an issue make sure you do not dereference
    invalid pointers (using references helps with that by requiring to be
    initilized although of course you may still have object lifetime issues).

    You seem to confuse "exceptions" as a general term/concept with C++
    exceptions language feature. They are not the same. What you do there is
    just to invoke "undefined behaivour". Your thread is very similar to one
    discussed here recently about exceptions (not) thrown on division by 0.

    --
    Dizzy
    http://dizzy.roedu.net
    Dizzy, Jan 11, 2007
    #3
  4. mlimber Guest

    Pete Becker wrote:
    > wrote:
    > >
    > > Please let me know how i could catch this exception
    > >

    >
    > In standard C++, if you don't throw it, you can't catch it. Hardware
    > "exceptions" are not the same thing as C++ "exceptions".


    Right. In non-standard C++, exceptions of a sort can sometimes thrown
    for errors of this kind (cf. Microsoft's "structured exceptions"), but
    for more on that, the OP will need to ask on a forum that deals with
    his specific compiler or platform.

    Cheers! --M
    mlimber, Jan 11, 2007
    #4
  5. Grizlyk Guest

    wrote:

    > This code simply terminates without catching an exception
    >
    > Please let me know how i could catch this exception


    Can not, but you can try use signal handlers (see signal.h), if your
    system generates and allows to catch signals in the case. I am not
    shure you will can continue execution after the error, but will can
    print error message.
    Grizlyk, Jan 14, 2007
    #5
    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. Herr Lucifer
    Replies:
    0
    Views:
    539
    Herr Lucifer
    Jun 21, 2004
  2. Ahmed Moustafa
    Replies:
    5
    Views:
    29,990
    Chris Smith
    Jul 14, 2004
  3. Paul Miller
    Replies:
    3
    Views:
    1,009
    Alex Martelli
    Nov 12, 2003
  4. Replies:
    3
    Views:
    599
    Sherm Pendley
    Apr 16, 2007
  5. Lie
    Replies:
    3
    Views:
    601
Loading...

Share This Page