Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people, just like you, are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
Solved

Catching system exceptions using C++ exception handling

Posted on 2002-06-26
8
365 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
ID: 7112338
It depends.  What operating system are you using?
0
 
LVL 86

Expert Comment

by:jkr
ID: 7112417
>>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
ID: 7113972
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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 7114071
>>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
 

Author Comment

by:lucand
ID: 7114383
SetUnhandledExceptionFilter() is working with multithreading.

Thanks jkr!
0
 
LVL 32

Expert Comment

by:jhance
ID: 7114396
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
ID: 7114441
Well, you could use the 'signal()' approach on Win32 also...
0
 

Author Comment

by:lucand
ID: 7114511
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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This article will show you some of the more useful Standard Template Library (STL) algorithms through the use of working examples.  You will learn about how these algorithms fit into the STL architecture, how they work with STL containers, and why t…
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 tutorial is to teach the user how to use functions in C++. The video will cover how to define functions, how to call functions and how to create functions prototypes. Microsoft Visual C++ 2010 Express will be used as a text editor an…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

789 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