<分区>
我知道在 C++
中使用 try
和 catch
block 进行异常处理。我想知道 C
中是否有此功能。所以,现在我知道 C
中的基本错误处理是由 setjmp/longjmp
完成的。
由于 setjmp/longjmp
不存在于 C++
中,可以假设 try/catch
更好吗?有哪些方式???
我可以使用 setjmp/longjmp
在 C
中实现 try/catch
功能。
这有什么不同??
标签 c++ c error-handling setjmp
<分区>
我知道在 C++
中使用 try
和 catch
block 进行异常处理。我想知道 C
中是否有此功能。所以,现在我知道 C
中的基本错误处理是由 setjmp/longjmp
完成的。
由于 setjmp/longjmp
不存在于 C++
中,可以假设 try/catch
更好吗?有哪些方式???
我可以使用 setjmp/longjmp
在 C
中实现 try/catch
功能。
这有什么不同??
最佳答案
我认为主要的区别是 try/catch 知道堆栈上的对象并且知道如何为分配在堆栈上的对象调用 dtors,而 setjmp 对此没有任何作用。
此外,用户界面也更加丰富,您可以定义多种异常类型并基于这些类型进行不同的行为
关于c++ - C++ try/catch 与 C setjmp/longjmp 有何不同?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14914692/