ANSI C相当于try / catch语句呢? [英] ANSI C equivalent of try/catch?

查看:203
本文介绍了ANSI C相当于try / catch语句呢?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一些C code我的工作,而且我发现错误,code运行时,但对如何做一个适当的try / catch(如C#或C小信息++ )。

例如,在C ++中我只是做的:

  {尝试
//一些东西
}
抓住(...)
{
//处理错误
}

但在ANSI C我有点失落。我尝试了一些网上搜索,但我没有看到有关如何做到这一点足够的信息/想我会在这里问的情况下,任何人都可以点我在正确的方向。

这里的code我跟(相当简单的,递归方法)的工作,愿与try / catch语句(或同等错误处理结构)来包装。

不过我的主要问题是只是如何做一个try / catch在ANSI C ...实施/例子并不一定是递归的。

 无效的getInfo(INT抵消,MYFILE :: MyItem *项)
{
    LL ::字符串myOtherInfo =本期特价货品> getOtherInfo();
    如果(myOtherInfo.isNull())
        myOtherInfo =;
    LL ::字符串getOne =;
    MYFILE :: ABC * ABC =本期特价货品> getOrig();
    如果(ABC!= NULL)
    {
        getOne = ABC-> getOne();
    }
    的for(int i = 0; I<抵消;我++)
    {
             的printf(发现数:%d,我);
    }
    如果(ABC!= NULL)
        ABC->发行();
    INT childCount =本期特价货品> getChildCount();
    偏移++;
    的for(int i = 0; I< childCount;我++)
        的getInfo(偏移,本期特价货品> getChild(ⅰ));
    本期特价货品>发行();
}


解决方案

通常情况下,你不知道。

这是可以使用的setjmp 的longjmp 来建立相当类似的东西try / catch语句,虽然没有这样的如析构函数用C事物或堆栈展开,所以RAII是出了问题。你可以用所谓的清除堆栈(例如见的Symbian / C ++),虽然它不是一个非常近似,这是一个大量的工作。甚至近似RAII

的常用方法,表示用C错误或者失败是返回指示成功状态的值。呼叫者检查返回值,并采取相应的行动。例如见标准的C函数:的printf 打开,的想法如何指定你的职责。

在混合C和C ++ code,则必须确保C ++异常从未达到C $ C $℃。在编写会从C调用C ++函数,捕获一切。

I have some C code I'm working with, and I'm finding errors when the code is running but have little info about how to do a proper try/catch (as in C# or C++).

For instance in C++ I'd just do:

try{
//some stuff
}
catch(...)
{
//handle error
}

but in ANSI C I'm a bit lost. I tried some online searches but I don't see enough info about how to make it happen / figured I'd ask here in case anyone can point me in the right direction.

Here's the code I'm working with (fairly simple, recursive method) and would like to wrap with try/catch (or equivalent error-handling structure).

However my main question is simply how to do a try / catch in ANSI C...the implementation / example doesn't have to be recursive.

void getInfo( int offset, myfile::MyItem * item )
{
    ll::String myOtherInfo = item->getOtherInfo();
    if( myOtherInfo.isNull() )
        myOtherInfo = "";
    ll::String getOne = "";
    myfile::Abc * abc = item->getOrig();
    if( abc != NULL )
    {
        getOne = abc->getOne();
    }
    for( int i = 0 ; i < offset ; i++ )
    {
             printf("found: %d", i);
    }
    if( abc != NULL )
        abc->release();
    int childCount = item->getChildCount();
    offset++;
    for( int i = 0 ; i < childCount ; i++ )
        getInfo( offset, item->getChild(i) );
    item->release();
}

解决方案

Generally, you don't.

It's possible to use setjmp and longjmp to build something fairly similar to try/catch, although there's no such thing in C as destructors or stack unwinding, so RAII is out of the question. You could even approximate RAII with a so-called "cleanup stack" (see for example Symbian/C++), although it's not a very close approximation, and it's a lot of work.

The usual way to indicate errors or failure in C is to return a value indicating success status. Callers examine the return value and act accordingly. See for example the standard C functions: printf, read, open, for ideas how to specify your functions.

When mixing C and C++ code, you must ensure that a C++ exception never reaches C code. When writing C++ functions that will be called from C, catch everything.

这篇关于ANSI C相当于try / catch语句呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆