why catch (...) can not catch such exception

Discussion in 'C++' started by John Black, Aug 19, 2004.

  1. John Black

    John Black Guest

    Hi,
    I have a code like this structure,

    try{
    .. some function call...
    }
    catch (...){
    return NULL;
    }

    during those function calls, the code has a bug which calls STL
    vector erase() to delete a NULL pointer, but the problem is catch(...)
    does not catch the error, instead I get a segment fault.

    What is the problem?
    John Black, Aug 19, 2004
    #1
    1. Advertising

  2. John Black wrote:
    > I have a code like this structure,
    >
    > try{
    > .. some function call...
    > }
    > catch (...){
    > return NULL;
    > }
    >
    > during those function calls, the code has a bug which calls STL
    > vector erase() to delete a NULL pointer, but the problem is catch(...)
    > does not catch the error, instead I get a segment fault.
    >
    > What is the problem?


    Hard to say. 'delete'ing a NULL pointer is explicitly allowed and
    is a NOP. Could it be that it's calling a member function using
    a pointer that is NULL, thus dereferencing it? That's not allowed.
    Exceptions produced by the execution environment do not always fall
    into C++ "catchable" category. Ask in the newsgroup for your OS or
    your compiler to see if there is a compiler- or platform-specific
    solution.

    Victor
    Victor Bazarov, Aug 19, 2004
    #2
    1. Advertising

  3. John Black

    Cy Edmunds Guest

    "John Black" <> wrote in message
    news:...
    > Hi,
    > I have a code like this structure,
    >
    > try{
    > .. some function call...
    > }
    > catch (...){
    > return NULL;
    > }
    >
    > during those function calls, the code has a bug which calls STL
    > vector erase() to delete a NULL pointer, but the problem is catch(...)
    > does not catch the error, instead I get a segment fault.
    >
    > What is the problem?
    >


    As Victor said, deleting a NULL pointer is not an error. There are many
    programming errors in C++ which result in the dreaded "undefined behavior".
    In such cases there is NO guarantee that an exception will be thrown. In
    fact there are no guarantees of any kind, hence the term.

    --
    Cy
    http://home.rochester.rr.com/cyhome/
    Cy Edmunds, Aug 19, 2004
    #3
  4. * John Black:
    > Hi,
    > I have a code like this structure,
    >
    > try{
    > .. some function call...
    > }
    > catch (...){
    > return NULL;
    > }
    >
    > during those function calls, the code has a bug which calls STL
    > vector erase() to delete a NULL pointer, but the problem is catch(...)
    > does not catch the error


    'catch' does not catch errors, it catches exceptions.

    ยง23.1/10: "no erase() ... function throws an exception".


    > instead I get a segment fault.
    >
    > What is the problem?


    That you call erase with a NULL pointer instead of a valid iterator.

    --
    A: Because it messes up the order in which people normally read text.
    Q: Why is it such a bad thing?
    A: Top-posting.
    Q: What is the most annoying thing on usenet and in e-mail?
    Alf P. Steinbach, Aug 19, 2004
    #4
  5. John Black

    John Black Guest

    Cy Edmunds wrote:

    > "John Black" <> wrote in message
    > news:...
    > > Hi,
    > > I have a code like this structure,
    > >
    > > try{
    > > .. some function call...
    > > }
    > > catch (...){
    > > return NULL;
    > > }
    > >
    > > during those function calls, the code has a bug which calls STL
    > > vector erase() to delete a NULL pointer, but the problem is catch(...)
    > > does not catch the error, instead I get a segment fault.
    > >
    > > What is the problem?
    > >

    >
    > As Victor said, deleting a NULL pointer is not an error. There are many
    > programming errors in C++ which result in the dreaded "undefined behavior".
    > In such cases there is NO guarantee that an exception will be thrown. In
    > fact there are no guarantees of any kind, hence the term.
    >
    > --
    > Cy
    > http://home.rochester.rr.com/cyhome/


    that's a little scary to know this...I had relied heavily on catch (...) to
    prevent my software from crash...is there any way to better handle error ?
    John Black, Aug 19, 2004
    #5
  6. John Black wrote:
    > Cy Edmunds wrote:
    >
    >
    >>"John Black" <> wrote in message
    >>news:...
    >>
    >>>Hi,
    >>> I have a code like this structure,
    >>>
    >>> try{
    >>> .. some function call...
    >>> }
    >>> catch (...){
    >>> return NULL;
    >>> }
    >>>
    >>> during those function calls, the code has a bug which calls STL
    >>>vector erase() to delete a NULL pointer, but the problem is catch(...)
    >>>does not catch the error, instead I get a segment fault.
    >>>
    >>> What is the problem?
    >>>

    >>
    >>As Victor said, deleting a NULL pointer is not an error. There are many
    >>programming errors in C++ which result in the dreaded "undefined behavior".
    >>In such cases there is NO guarantee that an exception will be thrown. In
    >>fact there are no guarantees of any kind, hence the term.
    >>
    >>--
    >>Cy
    >>http://home.rochester.rr.com/cyhome/

    >
    >
    > that's a little scary to know this...I had relied heavily on catch (...) to
    > prevent my software from crash...is there any way to better handle error ?


    Certain errors can only be handled by OS-specific means. Please ask in
    a newsgroup dedicated to your OS for more information.

    V
    Victor Bazarov, Aug 19, 2004
    #6
  7. John Black wrote:
    > ...
    > I have a code like this structure,
    >
    > try{
    > .. some function call...
    > }
    > catch (...){
    > return NULL;
    > }
    >
    > during those function calls, the code has a bug which calls STL
    > vector erase() to delete a NULL pointer, but the problem is catch(...)
    > does not catch the error, instead I get a segment fault.
    > ...


    'catch' does not catch "errors". It catches C++ exceptions thrown by
    'throw'. If you program crashes, 'catch' won't help you to prevent this.
    Some implementations might provide certain facilities that allow you to
    "convert" various hardware/OS-specific crash situations into C++
    exceptions (manually or automatically), but it depends on the concrete
    implementation, can be expensive and not always bulletproof.

    --
    Best regards,
    Andrey Tarasevich
    Andrey Tarasevich, Aug 19, 2004
    #7
  8. John Black

    jive Guest

    > that's a little scary to know this...I had relied heavily on catch (...)
    to
    > prevent my software from crash...is there any way to better handle error ?


    If your software crashes, it's faulty and should be treated as one. This
    sounds as an dangerous approach to program design.

    jive
    jive, Aug 20, 2004
    #8
  9. John Black

    Xenos Guest

    "jive" <> wrote in message
    news:cg47dd$2160$...
    > > that's a little scary to know this...I had relied heavily on catch (...)

    > to
    > > prevent my software from crash...is there any way to better handle error

    ?
    >
    > If your software crashes, it's faulty and should be treated as one. This
    > sounds as an dangerous approach to program design.
    >
    > jive
    >

    No, its not. It's called fault tolerant programming. Such a program should
    *never* crash. It should detect such detrimental problems and handle them
    accordingly. This "handling" can be anything appropriate for the design of
    the systems, such as restarting the process or rebooting the system.
    Usually when rebooting (which can be as quick as a few nanoseconds or take
    several minutes), a backup computer takes over until the master is health
    again. Before "fixing" the problem the system can save information on it in
    persistent storage so it can be invested during post-mission analysis.

    DrX
    Xenos, Aug 20, 2004
    #9
    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. Mr. SweatyFinger

    why why why why why

    Mr. SweatyFinger, Nov 28, 2006, in forum: ASP .Net
    Replies:
    4
    Views:
    863
    Mark Rae
    Dec 21, 2006
  2. Mr. SweatyFinger
    Replies:
    2
    Views:
    1,762
    Smokey Grindel
    Dec 2, 2006
  3. =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z

    Why can not catch the inner exception

    =?gb2312?B?yMvR1MLkyNXKx8zs0cSjrM37vKvM7NHEsru8+7z, Jun 7, 2007, in forum: Python
    Replies:
    2
    Views:
    357
    Guest
    Jun 7, 2007
  4. Marteno Rodia

    catch doesn't catch a thrown exception

    Marteno Rodia, Aug 3, 2009, in forum: Java
    Replies:
    5
    Views:
    549
    Daniel Pitts
    Aug 5, 2009
  5. Rob Sanheim

    why can't I catch this exception?

    Rob Sanheim, Aug 5, 2006, in forum: Ruby
    Replies:
    4
    Views:
    138
    Rob Sanheim
    Aug 5, 2006
Loading...

Share This Page