CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 5 of 5
  1. #1
    Join Date
    May 2000
    Location
    Germany
    Posts
    369

    Post Exception handling: Divison by zero, access violation, ...

    Hallo!
    I am looking for a solution to make my application more robust. So I need something to avoid unhandeld software and hardware exception. During my investigation I found something on the MSDN that is called "Structured Exception Handling". So I decided to use a combination of try..catch and AddVectoredExceptionHandler(exceptionHdl). 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. But there is another case that does not throw execption. For example:

    Code:
       char test[3];
       for( unsigned int i=0; i<1000000000; i++ )
          test[i] = 'a';
    This does not throw an exception but it enters the previously registered exception handler exceptionHdl. 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. But I need the call stack to analyse the problem. Inside the catch block I can determine the call stack with the stackwalker but inside the handler I can not. Is there a better way to handle and to recoginze that kind of errors and to get the call stack?

    Thanks in advance

  2. #2
    Join Date
    Nov 2006
    Posts
    120

    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.

  3. #3
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Exception handling: Divison by zero, access violation, ...

    Quote 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

  4. #4
    Join Date
    May 2000
    Location
    Germany
    Posts
    369

    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

  5. #5
    Join Date
    Dec 2005
    Posts
    642

    Arrow Re: Exception handling: Divison by zero, access violation, ...

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

    Quote 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
  •  





Click Here to Expand Forum to Full Width

Featured