开发者

Help with Try Catch

I'm using the GLUTesselator and every once in a while EndContour() fails so I did this:

         try
        {
            PolygonTesselator.End_Contour();
        }
        catch (int e)
        {
            renderShape = false;
       开发者_运维百科     return;
        }

Why would it still crash, it should perform the catch code right? How could I fix this?

Thanks


Does PolygonTesselator.End_Contour(); crash or throws it an exception?

Note that a real "crash" (segfault, illegal instruction etc.) does not throw an exception in the sense of C++.

In these cases, the CPU triggers an interrupt - this is also sometimes called exception, but has nothing to do with an C++ exception.

In C++ you are running on a real CPU - and not in a virtual machine like in Java where every memory violation results in language execption like NullPointerException or ArrayOutOfBoundsException.

In C/C++ a CPU exception / interrupt / trap is handled by the operating system and forwarded to the process as "signal". You can trap the signal but usually this does not help for crashes (SIGSEG, SIGILL, SIGFPU, etcc.).


You're getting SEH exception, or Structured Exception Handling. These exceptions are thrown by Windows under an entirely different system since SEH predates C++, and can't be caught by a standard C++ catch block (MSVC does however provide SEH catching).

Alternatively, if you're on Unix, the kernel doesn't use C++ exceptions either. It uses signals. I don't pretend to understand signals, since I don't develop for Unix, but I'm very sure they're not C++ exceptions.

You've violated a hardware rule that you can't de-reference a NULL pointer. You'll get an OS-level error, by exception or signal. You can't catch these things in a C++ catch block just like that.

Edit: If you're using MSVC on Windows and have a recent compiler, you CAN enable /EHa, which allows you to catch Structured Exceptions as well as C++ exceptions. I wrote this code that functions as shown:

int main() {
    std::string string = "lolcakes";
    try {
        Something s;
        int i, j;
        i = 0; j = 1;
        std::string input;
        std::cin >> input;
        if (input == "Structured")
            int x = j / i;
        else
            throw std::runtime_error("Amagad hai!");
    }
    catch(std::runtime_error& error) {
        std::cout << "Caught a C++ exception!" << std::endl;
    }
    catch(...) {
        std::cout << "Caught a structured exception!" << std::endl;
    }
    return main();
}

Structured Exceptions include Access Violations, your particular error.


Two things :

  • If you are under VC 6 or more recent, use __try{}__except(EXCEPTION_EXECUTE_HANDLER){} instead. It will catch the SE. try/catch only catches C++ exceptions
  • I've had a hard time with GLU, so I can tell you this : if you can set the normal, SET IT. But otherwise, it's true that it's quite solid.


Since all you want to do when the exception arrives is set 'renderShape' to false I'd recomment the following code:

     try
    {
        PolygonTesselator.End_Contour();
    }
    catch (...) // takes any type of exception
    {
        renderShape = false;
        return;
    }

That said of course if you really can't fix the source to the exception itself!!


Why would it still crash, it should perform the catch code right? How could I fix this?

Most likely because you are not actually catching what is being thrown. Are you sure that PolygonTesselator.End_Contour(); throws an int?


If your program fails with the message error reading from location 0x000000000, then it is not failing because of an exception, but because of a segfault. You can use the segvcatch utility to convert segfaults into catchable exceptions. Check out their examples.


As mentioned, this is an SEH Exception.
If you are using Visual Studio, you can configure your project to catch SEH exceptions in your try/catch block.

Project Properties > C/C++ > Code Generation > Enable C++ Exceptions > Yes with SEH Exceptions

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜