Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

howto catch access violation

Status
Not open for further replies.

AMosmann

Programmer
May 27, 2003
82
0
0
DE
I look for a possibility to catch an access violation like

try
int *p;
*p=1;
catch (CWhatever *e) {
if (e->m_cause==CWhatever::ecAccessViolation){
TRACE("There are big problems, lucky guy");
}
e->Delete();
}

Does anyone have such an Idea?
(-:



Greetings Andreas
 
That does not really reflect the typical usage of exception handling. Exceptions are generally caught by outer level code not inner level code. Then in that case what would you do. However if you want to try I believe structured exception handling can catch those errors but I don’t know how helpful that will be.

Generally when using pointers you always initialize them and always check them.
Code:
int* p = NULL;
if( p){
....
}
If you want DEBUG time notification of problems that is what asserstions are for
Code:
// C++
assert( p);
// MFC
ASSERT( p);

Also of course minimize the use of pointers by design. This eliminates the majority (99%) of those errors through design rather than error handling.

-pete
 
to explain it a bit more

I have to use some modules programmed by other programmers.
To avoid that the complete application crashes without any senseful hint I would like to catch nearly all possible exceptions when starting that modules.

try
BadFunctionThatCouldRaiseAccessViolation();
catch (CWhatever *e) {
if (e->m_cause==CWhatever::ecAccessViolation){
TRACE("There are big problems, lucky guy");
}
e->Delete();
}

;-)


Greetings Andreas
 
As I stated I believe structured exception handling is the only way to catch access violations. However it does not work the way you want. Basically you set a handler function that gets called. So you don’t get to use a try/catch block where you want to catch the exception.

So I guess the short answer is No.

>> I have to use some modules programmed by other programmers.

If you have the source you need to fix it. If you don’t have the source then if you don’t want the application to crash you should not use buggy libraries.


-pete
 
thx for your answer, but I dont have to choose between the libraries, the boss [yoda] is doing that, I "only" have to manage that.
Libraries exist longer than I work in this company, are very complex and not too easy to debug, cause errors only come from time to time.
But, if the error happens in my part - I am the looser [sad]
So I try to catch as catch can [pc2]

Hope dies last


Greetings Andreas
 
catch(...) will catch an access violation - but you won't know what caused the exception.

/JOlesen
 
Can you explain it more?
catch(what)?
I tried CException*, but it did not work


Greetings Andreas
 
Try it out :




#include <cstdio>
#include <cstring>

void main(void)
{
char * psrc = &quot;abc&quot;;
try {
strcpy(0, psrc);
}
catch(...) {
printf(&quot;\nGotYah&quot;);
}
}
 
Sure, but that's not what he posted. Try this
Code:
int* p;
try{
	*p = 10;
}catch(...){
	TRACE(&quot;GotYahNot\r\n&quot;);
}

[wink]

-pete
 
hi pete
>Sure, but that's not what he posted. Try this

>int* p;
>try{
> *p = 10; //;-)
>}catch(...){
> TRACE(&quot;GotYahNot\r\n&quot;);
>}

thx pete and /JOlesen
[cheers]

Greetings Andreas
 
you can use SEH frame:
__try
{
}__except(.......)
{
}

Ion Filipski
1c.bmp

ICQ: 95034075
AIM: IonFilipski
filipski@excite.com
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top