- iOS/Objective-C 元类和类别
- objective-c - -1001 错误,当 NSURLSession 通过 httpproxy 和/etc/hosts
- java - 使用网络类获取 url 地址
- ios - 推送通知中不播放声音
这是我的代码:
#include <string>
#include <tr1/functional>
using namespace std;
using namespace std::tr1;
using namespace std::tr1::placeholders;
class Event
{
public:
typedef std::tr1::function<void()> Handler;
void set(Handler h)
{
m_handler = h;
}
template<typename T, typename F>
void set(T * obj, F memfn)
{
set(std::tr1::bind(memfn, obj));
}
void operator()()
{
m_handler();
}
static void fire(Event * event) throw ()
{
(*event)();
}
Handler m_handler;
};
class BuggyHandler
{
public:
BuggyHandler()
{
}
BuggyHandler(Event * b) :
bar(b)
{
bar->set(this, &BuggyHandler::HandleEvent);
}
void HandleEvent()
{
// throw std::length_error
std::string().append(std::numeric_limits<size_t>::max(), '0');
}
private:
Event * bar;
};
void get_correct_stacktrace()
{
Event bar;
BuggyHandler handler(&bar);
bar();
}
void get_incorrect_stacktrace()
{
Event bar;
BuggyHandler handler(&bar);
Event::fire(&bar);
}
int main(int argc, char **argv)
{
int opt = atoi(argv[1]);
if (opt)
get_correct_stacktrace();
else
get_incorrect_stacktrace();
}
当我调用 ./test 1 时,我可以从核心获得正确的堆栈跟踪:
#0 0xffffe410 in __kernel_vsyscall ()
#1 0xf7d028d0 in raise () from /lib/libc.so.6
#2 0xf7d03ff3 in abort () from /lib/libc.so.6
#3 0xf7ede880 in __gnu_cxx::__verbose_terminate_handler () from /usr/lib/libstdc++.so.6
#4 0xf7edc2a5 in std::exception::what () from /usr/lib/libstdc++.so.6
#5 0xf7edc2e2 in std::terminate () from /usr/lib/libstdc++.so.6
#6 0xf7edc41a in __cxa_throw () from /usr/lib/libstdc++.so.6
#7 0xf7e73c6f in std::__throw_length_error () from /usr/lib/libstdc++.so.6
#8 0xf7eb9a17 in std::string::append () from /usr/lib/libstdc++.so.6
#9 0x08049b96 in BuggyHandler::HandleEvent (this=0xffc26c9c) at /home/liangxu/release/server_2.0/test/src/test.cc:54
#10 0x08049857 in get_correct_stacktrace () at /home/liangxu/release/server_2.0/test/src/test.cc:67
#11 0x080498e0 in main (argc=Cannot access memory at address 0x5ac6) at /home/liangxu/release/server_2.0/test/src/test.cc:81
抛出异常的位置是test.cc:54
当我调用 ./test 0 时,我可以从核心获取不正确的堆栈跟踪:
#0 0xffffe410 in __kernel_vsyscall ()
#1 0xf7d508d0 in raise () from /lib/libc.so.6
#2 0xf7d51ff3 in abort () from /lib/libc.so.6
#3 0xf7f2c880 in __gnu_cxx::__verbose_terminate_handler () from /usr/lib/libstdc++.so.6
#4 0xf7f2a2a5 in std::exception::what () from /usr/lib/libstdc++.so.6
#5 0xf7f2a2e2 in std::terminate () from /usr/lib/libstdc++.so.6
#6 0xf7f2a305 in std::exception::what () from /usr/lib/libstdc++.so.6
#7 0xf7f29d98 in __cxa_call_unexpected () from /usr/lib/libstdc++.so.6
#8 0x080497eb in get_incorrect_stacktrace () at /home/liangxu/release/server_2.0/test/src/test.cc:30
#9 0x080498f5 in main (argc=Cannot access memory at address 0x5adf) at /home/liangxu/release/server_2.0/test/src/test.cc:83
没有抛出异常的位置。
我的编译器是“gcc (GCC) 4.1.2 20070115 (prerelease) (SUSE Linux)”
如果使用“-fno-exceptions”编译,两种方法都会生成正确的堆栈跟踪。
这是什么原因?
最佳答案
两个堆栈跟踪都是正确的。
当您调用 Event::fire
时, 异常抛出在 HandleEvent
并且堆栈展开发生直到它遇到 fire
它具有那个异常规范。
如果您不知道异常规范的真实行为,您可以在这里阅读:http://www.gotw.ca/publications/mill22.htm
基本上,规范throw ()
如果其中一个包含调用抛出,则确实保证此方法不会因悲惨地失败而抛出。当堆栈展开试图退出此方法时,它会检查异常规范,发现它不匹配,然后调用 std::unexpected
从展开的当前位置,因此 __cxa_call_unexpected ()
在 get_incorrect_stacktrace ()
之后的堆栈跟踪中.
在大多数情况下,在 C++ 中使用异常规范是无用的,因为提供保证的代价是程序的一般失败是抛出而不应该抛出的东西。
关于c++ - 未捕获的 std::exception 在核心中不正确的堆栈跟踪,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8738301/
这个问题在这里已经有了答案: Python try...except comma vs 'as' in except (5 个回答) 关闭7年前。 在python中,有两种方法可以捕获异常 excep
在 Java 中,我有一个从 Exception 扩展的异常类,但是每当我抛出它时,编译器都会说它需要被捕获/必须声明方法 throws异常。 当我使用从 Exception 扩展的 RuntimeE
我有一组用户、组以及用户和组之间的映射。我有各种操作这些集合的函数,但是不能为不存在的用户添加用户组映射,也不能删除仍然有用户作为成员的组等。 所以基本上我希望这些函数抛出必须由调用者明确处理的“异常
我正在尝试使用上载控件上载20兆的文件,并且在Visual Studio的内置Web服务器上可以正常工作,但是一旦将其发布到生产服务器(我无权访问),我总是收到以下错误消息: Server Error
我想断言运行某些代码时会引发特定异常(SSLHandshakeException)。 assertThatThrownBy(() -> { // some code }).is
这个问题我暂时解决不了。我很乐意提供一些建议。 当我尝试抛出异常时(我自己创建了一个 Java 风格的异常) throw Exception (); 编译器提出抗议: DataTypes/Date.c
我有以下文件: from fabric.api import env, execute, run env.hosts = ['1.2.3.4'] def taskA(): run('ls')
我正在阅读一些包含类似于以下功能的源代码: def dummy_function(): try: g = 1/0 except Exception as e:
根据标准 ML 的定义(修订版): The idea is that dynamic evaluation of a non-expansive expression will neither gen
当 GHCi 在运行时发现调用产生的值与函数的模式匹配不匹配时,有没有办法让 GHCi 产生更好的异常消息? 它目前给出了产生非详尽模式匹配的函数的行号,虽然有时会有所帮助,但确实需要一轮调试,有时我
我有一个最佳实践问题。我意识到这是主观的,但想问问比我更聪明的人,这是否是一种常见的编程实践。 如果您有一种不希望干扰应用程序重要功能的非关键方法,那么使用这样的错误接收器是否常见? Try
在编程中,异常是否总是错误(被零除,访问冲突等等)? 如果不是,您能否提供不是错误的异常示例? 谢谢。 最佳答案 异常通常用于管理错误,它们使错误处理更加容易,但它们并不总是错误。 任何需要单独代码路
我很想知道 OCaml 运行时如何处理异常以使它们如此轻量。他们是使用 setjmp/longjmp 还是在每个函数中返回一个特殊值并传播它? 在我看来,longjmp会给系统带来一点压力,但只有在引
在我的 C# 代码中,我可以访问 MyNamespace.Exception 以及 System.Exception。当我想捕获其中一个异常时,理想情况下我会完全限定要捕获的异常或使用别名来明确说明。
我正在使用 Visual C++ 2005 Express Edition 并遇到以下链接器错误: 19>mylib1.lib(mylibsource1.obj) : error LNK2019: u
这个问题在这里已经有了答案: Is there "Break on Exception" in IntelliJ? (6 个回答) 关闭7年前。 我想在调试器中运行我的测试套件并中断任何意外异常,但是
Like in this picture 我知道它们都可以正常工作,但我只是想知道它们之间有何不同? PS:我是初学者。 最佳答案 A LogEvent可以同时包含消息和异常。如果您使用第一种形式:
我知道避免 Doctrine 上的异常似乎是一种奇怪的行为,但我需要这样做,因为我在一个旧项目中工作,过去有人执行了一些迁移,然后他决定删除它,所以现在复制起来很复杂本地生产环境没有崩溃,这就是为什么
我想创建一个名为 SecurityException 的新异常。 我应该把代码放在哪里? class SecurityException extends CakeException {}; 谢谢! 最
我一直在使用throw new Exception("...")在我的代码中,因为我找不到其他可以使用的东西。我正在寻找像 C++'s 这样的东西 out_of_range 和 logic_error
我是一名优秀的程序员,十分优秀!