C++里面异常处理官方例程运行有错误

发布于 2020-11-17 17:04:41

程序为

#include <math.h>
#include <stdlib.h>

#define MIN_VALUE                 (1e-4)
#define IS_DOUBLE_ZERO(d)         (abs(d) < MIN_VALUE)

double div_func(double x, double y)
{
    if (IS_DOUBLE_ZERO(y))
    {
        throw y;                                           /* throw exception */
    }

    return x / y;
}
void throw_exceptions(void *args)
{
    try
    {
        div_func(6, 3);
        rt_kprintf("there is no err\n");
        div_func(4, 0);                                   /* create exception*/
        rt_kprintf("you can run here\n");
    }
    catch(double)                                         /* catch exception */
    {
        rt_kprintf("error of dividing zero\n");
    }
}

MSH_CMD_EXPORT(throw_exceptions, throw cpp exceptions);

运行时报错为

\ | /
- RT -     Thread Operating System
 / | \     4.0.3 build Nov 17 2020
 2006 - 2020 Copyright by rt-thread team
RTT Control Block Detection Address is 0x200024b0
msh >throw_exceptions
there is no err
thread:tshell   abort!
  • 经过调试发现,程序中throw的y没被catch,转而执行到rtthread/components/libc/compliers/newlib/syscalls.c中的abort函数
void abort(void)
{
    if (rt_thread_self())
    {
        rt_thread_t self = rt_thread_self();

        rt_kprintf("thread:%-8.*s abort!\n", RT_NAME_MAX, self->name);
        rt_thread_suspend(self);

        rt_schedule();
    }

    while (1);
}
  • 请问是为什么?

查看更多

关注者
0
被浏览
77
0 个回答
暂无答案,快来添加答案吧

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览