-
September 6th, 2007, 08:35 AM
#1
-
September 6th, 2007, 11:07 AM
#2
Re: Exception handling: Divison by zero, access violation, ...
I apologise if this is not directly relevant but you might want to consider handling win32 exceptions as C++ exceptions. To do this you need the _set_se_translator function: http://msdn2.microsoft.com/en-us/lib...h5(VS.80).aspx
You could also consider using std::vector instead of an array and indexing with the .at method instead of using []. There is no "array bounds exceeded" check in C++ and the only way you are going to cause an exception is by corrupting something which subsequently causes another different problem. The .at method of the standard template classes guarantees a bounds check.
-
September 6th, 2007, 12:21 PM
#3
Re: Exception handling: Divison by zero, access violation, ...
Originally Posted by kakalake
The problem now is that division by zero (3/0), a wrong parameter (strlen(NULL)), wrong method call (CTest *pTest = NULL; pTest->foo() , wrong function call (FuncPtr ptr = NULL; ptr(1,2) are easy to handle problems because they all trow c++ exceptions.
Actually, none of these throws C++ exceptions. First strlen() is a C function, there is no exception handling for that. Neither are divisions by zero, as floating point processors can control what happens if you get a divide by 0.
The only C++ exceptions that are guaranteed to be thrown are
1) The exceptions that the ANSI/ISO C++ language spec says are thrown. These exceptions include things such as operator new[] running out of memory, vector::at() (as mentioned by another poster), dynamic_cast<> on a wrong reference type, etc.
and
2) User-defined C++ exceptions that your application or third-party library throws explicitly.
For 2), those user-defined exceptions may include the Windows SEH exception type. However, either you have to set them up yourself, or the compiler runtime may set them up. But they are still user-defined exceptions that are not included automatically in a C++ program.
So in truth, the only exceptions you are guaranteed to get right out of the box without adding any of your own thrown exceptions are the ones defined in 1) above.
Regards,
Paul McKenzie
-
September 11th, 2007, 03:26 AM
#4
Re: Exception handling: Divison by zero, access violation, ...
Hi!
If I use SEH (__try, __except) to catch a access violation exception then there is no possibility for me to write the address where the exception occur down. The problem is that if I try to write the address into a file then this code raises another exception. This is my exception-handler:
Code:
int exception_filter(std::ostream& os, EXCEPTION_POINTERS * pex, DWORD exceptionCode, CExceptionHandlingDlg *t )
{
if(exceptionCode!=EXCEPTION_ACCESS_VIOLATION)
return EXCEPTION_CONTINUE_SEARCH;
address = (int)pex->ExceptionRecord->ExceptionAddress;
os << address << std::endl; //this does not work
os.flush();
return EXCEPTION_CONTINUE_EXECUTION;
}
Is there a possibility to write the information down. I need the address where the exception raised.
Thanks
-
September 11th, 2007, 07:22 AM
#5
Re: Exception handling: Divison by zero, access violation, ...
Originally Posted by kakalake
Code:
char test[3];
for( unsigned int i=0; i<1000000000; i++ )
test[i] = 'a';
..But my problem with this solution is that this handler has been called after the application and all other threads were terminated and so there is no possiblity for me to determine the right call stack.
This code sample completely obliterates the stack, so you can't expect to be able to do a call stack trace after this.
Originally Posted by kakalake
Code:
int exception_filter(std::ostream& os, EXCEPTION_POINTERS * pex, DWORD exceptionCode, CExceptionHandlingDlg *t )
I need the address where the exception raised.
The address where the exception was raised is in pex->ExceptionRecord->ExceptionAddress. The full processor context where the exception was raised is in pex->ContextRecord, but this struct is processor-specific.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|