handling invalid memory access exception .

M

mangesh

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 .
 
J

Jakob Bieling

mangesh said:
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
 
B

benben

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
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
 
R

Rolf Magnus

mangesh said:
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.
 
S

Scott J. McCaughrin

: > 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?
 
F

Frank Puck

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.
 
M

mangesh

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 .
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,768
Messages
2,569,575
Members
45,053
Latest member
billing-software

Latest Threads

Top