简体   繁体   English

帮助Try Catch

[英]Help with Try Catch

I'm using the GLUTesselator and every once in a while EndContour() fails so I did this: 我正在使用GLUTesselator并且每隔一段时间EndContour()失败所以我这样做了:

         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(); 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++. 请注意,真正的“崩溃”(段错误,非法指令等)不会在C ++意义上引发异常。

In these cases, the CPU triggers an interrupt - this is also sometimes called exception, but has nothing to do with an C++ exception. 在这些情况下,CPU会触发中断 - 这有时也称为异常,但与C ++异常无关。

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. 在C ++中,您运行的是真正的CPU - 而不是像Java中那样的虚拟机,其中每次内存违规都会导致语言执行,如NullPointerException或ArrayOutOfBoundsException。

In C/C++ a CPU exception / interrupt / trap is handled by the operating system and forwarded to the process as "signal". 在C / C ++中,CPU异常/中断/陷阱由操作系统处理并作为“信号”转发到进程。 You can trap the signal but usually this does not help for crashes (SIGSEG, SIGILL, SIGFPU, etcc.). 您可以捕获信号,但通常这对崩溃没有帮助(SIGSEG,SIGILL,SIGFPU等)。

You're getting SEH exception, or Structured Exception Handling. 您正在获得SEH例外或结构化异常处理。 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). Windows在完全不同的系统下抛出这些异常,因为SEH早于C ++,并且不能被标准C ++ catch块捕获(但MSVC确实提供了SEH捕获)。

Alternatively, if you're on Unix, the kernel doesn't use C++ exceptions either. 或者,如果您使用的是Unix,则内核也不会使用C ++异常。 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. 我不假装理解信号,因为我不为Unix开发,但我非常确定它们不是C ++异常。

You've violated a hardware rule that you can't de-reference a NULL pointer. 您违反了无法取消引用NULL指针的硬件规则。 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. 你不能像这样在C ++ catch块中捕获这些东西。

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. 编辑:如果您在Windows上使用MSVC并拥有最新的编译器,则可以启用/ EHa,它允许您捕获结构化异常以及C ++异常。 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. 结构化异常包括访问冲突,您的特定错误。

Since all you want to do when the exception arrives is set 'renderShape' to false I'd recomment the following code: 因为在异常到达时你想要做的就是将'renderShape'设置为false我会推荐以下代码:

     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(); 你确定PolygonTesselator.End_Contour(); throws an int? 抛出一个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. 如果您的程序因error reading from location 0x000000000的消息error reading from location 0x000000000而失败,那么它不会因为异常而失败,但是由于段错误。 You can use the segvcatch utility to convert segfaults into catchable exceptions. 您可以使用segvcatch实用程序将segfaults转换为可捕获的异常。 Check out their examples. 看看他们的例子。

As mentioned, this is an SEH Exception. 如上所述,这是SEH异常。
If you are using Visual Studio, you can configure your project to catch SEH exceptions in your try/catch block. 如果您使用的是Visual Studio,则可以将项目配置为捕获try / catch块中的SEH异常。

Project Properties > C/C++ > Code Generation > Enable C++ Exceptions > Yes with SEH Exceptions 项目属性> C / C ++>代码生成>启用C ++异常> 是的SEH异常

Two things : 两件事情 :

  • If you are under VC 6 or more recent, use __try{}__except(EXCEPTION_EXECUTE_HANDLER){} instead. 如果您使用的是VC 6或更新版本,请使用__try {} __,但(EXCEPTION_EXECUTE_HANDLER){}除外。 It will catch the SE. 它会赶上SE。 try/catch only catches C++ exceptions try / catch仅捕获C ++异常
  • I've had a hard time with GLU, so I can tell you this : if you can set the normal, SET IT. 我和GLU度过了一段艰难的时光,所以我可以告诉你:如果你可以设置正常,请设置IT。 But otherwise, it's true that it's quite solid. 但除此之外,它确实很稳固。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM