Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > handling invalid memory access exception .

Reply
Thread Tools

handling invalid memory access exception .

 
 
mangesh
Guest
Posts: n/a
 
      04-08-2006
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 .

 
Reply With Quote
 
 
 
 
Jakob Bieling
Guest
Posts: n/a
 
      04-08-2006
mangesh <(E-Mail Removed)> 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)


 
Reply With Quote
 
 
 
 
benben
Guest
Posts: n/a
 
      04-08-2006
> 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<>: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

 
Reply With Quote
 
Rolf Magnus
Guest
Posts: n/a
 
      04-08-2006
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.

 
Reply With Quote
 
Scott J. McCaughrin
Guest
Posts: n/a
 
      06-23-2006
Jakob Bieling <(E-Mail Removed)> wrote:
: mangesh <(E-Mail Removed)> 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?


 
Reply With Quote
 
Frank Puck
Guest
Posts: n/a
 
      06-26-2006
> 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.

 
Reply With Quote
 
mangesh
Guest
Posts: n/a
 
      06-28-2006

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 .

 
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
signal handling and (structured) exception handling Peter C++ 34 10-17-2009 10:03 AM
Exception of type 'System.Web.HttpUnhandledException' wasthrown.Exception has been thrown by the target of an invocation.System.WebSystem.Exception jobs ASP .Net 1 11-16-2007 05:57 PM
Invalid access to memory location. (Exception from HRESULT: 0x8007 =?Utf-8?B?SUthdHphdg==?= ASP .Net 0 06-11-2007 08:21 AM
while executing my client program i get the exception javax.naming.LinkException: [Root exception is javax.naming.LinkException: [Root exception is javax.naming.NameNotFoundException: remaining if plz anybody know how to solve this problem then mahesh Java 0 03-08-2007 12:26 PM
SoapExtension for Global Exception handling; but prevent exception from propagating!! VSK ASP .Net Web Services 0 07-29-2003 05:39 PM



Advertisments