Solved

Catching system exceptions using C++ exception handling

Posted on 2002-06-26
8
362 Views
Last Modified: 2008-03-17
Is that posible to catch system exceptions like "access violation" by using C++ exception handling? In that case what will be the thrown object that I can use into the catch() statement? I don't want to use the catch(...) because I need more information about the exception.

Thanks
0
Comment
Question by:lucand
  • 3
  • 3
  • 2
8 Comments
 
LVL 32

Expert Comment

by:jhance
Comment Utility
It depends.  What operating system are you using?
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
>>It depends.

Actually, there are just 2 ways - SEH (Win32) or signals (all POSIX).

So, in code

(SEH)
__try {

 // code here

} __except ( EXCEPTION_ACCESS_VIOLATION == GetExceptionCode()) {

throw new XcpMyAccVio ();
}

(POSIX)
void my_sigsegv_handler ( int) {

throw new XcpMyAccVio ();
}

signal ( SIGSEGV, my_sigsegv_handler);
0
 

Author Comment

by:lucand
Comment Utility
I use WindowsNT,2000

I allready tryed signals and _set_se_translator() but not working into a multithreading program(any sugestions?).

SEH is not specifically designed for C++ and I get the following compilation error: "Cannot use __try in functions that require object unwinding"

0
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
Comment Utility
>>SEH is not specifically designed for C++ and I get the
>>following compilation error: "Cannot use __try in
>>functions that require object unwinding"

Yes, but this is not a problem. You would overcome this by simply not using the SEH frame handler in functions that have non-scalar data types.

Even easier, you could use 'SetUnhandledExceptionFilter()', e.g.

LONG
UnhandledExceptionFilter(
  STRUCT _EXCEPTION_POINTERS *ExceptionInfo   // address of
                                              // exception info
)
{
 if ( EXCEPTION_ACCESS_VIOLATION == GetExceptionCode() {

  throw new XcpMyAccVio ();
 }

 return EXCEPTION_EXECUTE_HANDLER;
}
 

0
6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

 

Author Comment

by:lucand
Comment Utility
SetUnhandledExceptionFilter() is working with multithreading.

Thanks jkr!
0
 
LVL 32

Expert Comment

by:jhance
Comment Utility
Hmmm, like I said originally, "It depends...."

The accepted solution is NOT C++ but rather a Windows-ism.
0
 
LVL 86

Expert Comment

by:jkr
Comment Utility
Well, you could use the 'signal()' approach on Win32 also...
0
 

Author Comment

by:lucand
Comment Utility
Jhance you are right, that is not a pure C++ solution but it's working fine in Windows.

signal() is not working if the exception is into a child thread or if signal() is called from a DLL

Thanks
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
This article shows you how to optimize memory allocations in C++ using placement new. Applicable especially to usecases dealing with creation of large number of objects. A brief on problem: Lets take example problem for simplicity: - I have a G…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will learn how to clear a vector as well as how to detect empty vectors in C++.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now