handling invalid memory access exception .

Discussion in 'C++' started by mangesh, Apr 8, 2006.

  1. mangesh

    mangesh Guest

    1 - How to cach invalid memory access exception ? Does standard library
    provide any help ?

    2 - Also when one write
    catch(...)
    {
    //...........
    }

    what is wirtten inside catch block . How do we know type of exception
    thrown in such case ?

    Regards
    Mangesh Sawant .
     
    mangesh, Apr 8, 2006
    #1
    1. Advertising

  2. mangesh <> wrote:

    > 1 - How to cach invalid memory access exception ? Does standard
    > library provide any help ?


    Instead of catching invalid memory access exceptions, you should
    find the bug in your code and fix it, thus not getting the exception in
    the first place.

    > 2 - Also when one write
    > catch(...)
    > {
    > //...........
    > }
    >
    > what is wirtten inside catch block . How do we know type of exception
    > thrown in such case ?


    You don't. You used "..." after all. If you want to handle different
    types of exceptions, use different catch handlers:

    try
    {
    }
    catch (std::exception const& e)
    {
    }
    catch (char const* e)
    {
    }
    catch (...)
    {
    }

    hth
    --
    jb

    (reply address in rot13, unscramble first)
     
    Jakob Bieling, Apr 8, 2006
    #2
    1. Advertising

  3. mangesh

    benben Guest

    > 1 - How to cach invalid memory access exception ? Does standard library
    > provide any help ?


    Not every runtime error will trigger a C++ exception throwing. Indeed,
    the behavior is undefined or platform dependent. In some systems, memory
    access violation results in immediate core dump. So the best you can do
    is to make sure you don't access invalid memory.

    Accessing elements in standard containers through iterator can throw an
    exception if the implementation uses a checked iterator. You should
    consult the documentation. In general, you should always check the
    iterator before you dereference it.

    There are also safe alternatives to accessing elements in the standard
    library. For example, use vector<>::at rather than vector<>::eek:perator[]
    is the performance drop isn't an issue to your program.

    >
    > 2 - Also when one write
    > catch(...)
    > {
    > //...........
    > }
    >
    > what is wirtten inside catch block . How do we know type of exception
    > thrown in such case ?


    You don't. That's why you should only use this when you don't care about
    the type of exception thrown.

    >
    > Regards
    > Mangesh Sawant .
    >

    Regards,
    Ben
     
    benben, Apr 8, 2006
    #3
  4. mangesh

    Rolf Magnus Guest

    mangesh wrote:

    > 1 - How to cach invalid memory access exception ?


    In standard C++, the behavior is undefined if you try to do an invalid
    memory access. Some implementation might throw an exception, but I think
    most don't. However, there are special memory debugers like valgrind that
    you can run your program in, and it can give you detailed information about
    any irregularities.

    > Does standard library provide any help ?


    No.

    > 2 - Also when one write
    > catch(...)
    > {
    > //...........
    > }
    >
    > what is wirtten inside catch block . How do we know type of exception
    > thrown in such case ?


    We don't.
     
    Rolf Magnus, Apr 8, 2006
    #4
  5. Jakob Bieling <> wrote:
    : mangesh <> wrote:

    : > 1 - How to cach invalid memory access exception ? Does standard
    : > library provide any help ?


    How about using signal() or sigset() with SIGSEGV?

    : Instead of catching invalid memory access exceptions, you should
    : find the bug in your code and fix it, thus not getting the exception in
    : the first place.

    You are assuming the invalid memory access is due to a bug in the code.
    What if it is not?
     
    Scott J. McCaughrin, Jun 23, 2006
    #5
  6. mangesh

    Frank Puck Guest

    > How about using signal() or sigset() with SIGSEGV?
    >
    > : Instead of catching invalid memory access exceptions, you should
    > : find the bug in your code and fix it, thus not getting the exception in
    > : the first place.
    >
    > You are assuming the invalid memory access is due to a bug in the code.
    > What if it is not?



    accessing memory mapped files together with sparse files and the system
    running out of disk space to back this sparse file may cause exceptions on
    Windows and signals on UNIXs.
    I certainly prefer exceptions over signals.
     
    Frank Puck, Jun 26, 2006
    #6
  7. mangesh

    mangesh Guest

    Thanks for reply .
    One qwestion , if exception is not due to mistake in my code then what
    else can
    cause exception . Can included libraries cause excption , these are
    generaly well
    tested .

    Regards ,
    Mangesh Sawant .
     
    mangesh, Jun 28, 2006
    #7
    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. Bikash
    Replies:
    5
    Views:
    443
    Daniel T.
    Jul 22, 2004
  2. Tony Johansson
    Replies:
    3
    Views:
    324
    Old Wolf
    Aug 13, 2005
  3. =?Utf-8?B?SUthdHphdg==?=
    Replies:
    0
    Views:
    1,278
    =?Utf-8?B?SUthdHphdg==?=
    Jun 11, 2007
  4. Peter
    Replies:
    34
    Views:
    1,940
    James Kanze
    Oct 17, 2009
  5. VSK
    Replies:
    0
    Views:
    248
Loading...

Share This Page