| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1183 人关注过本帖
标题:关于try catch
只看楼主 加入收藏
leshui
Rank: 2
等 级:论坛游民
威 望:1
帖 子:7
专家分:17
注 册:2022-10-24
结帖率:50%
收藏
已结贴  问题点数:20 回复次数:3 
关于try catch
#include <iostream>
//#include <fstream>
#include <exception>
#include <string>
#include "boost/lexical_cast.hpp"
#include <unistd.h>
using namespace std;
extern char *optarg;
unsigned tryfunc(char *a)
{
    int intnum;
    try {
        std::cout<<"ready to throw!\n";
        if(!(intnum=boost::lexical_cast<unsigned>(optarg)))
        throw (optarg);
        #如果有错不传到catch
    }
    catch(...)
    {
        exception e;
        std::cout<<"-------------------wrong:"<<e.what();
        throw ;
        }
    return intnum;
   
}


int main(int argc,char *argv[])
{  
    int opt,rmint=5;
    string help="wrong!\n";
    while ((opt = getopt(argc, argv, ":r:")) != -1)
  {
   switch (opt)
     {
     case 'r':
     rmint=tryfunc(optarg);
     break;
     default: /* '?' */
     std::cout<<help<<endl;
     return 0;
      }
    }
    std::cout<<"you input optarg:"<<rmint<<endl;
 }
#以下是我的运行结果
wei@wei:~/bin$ ./mn -ra
ready to throw!
terminate called after throwing an instance of 'boost::exception_detail::clone_impl<boost::exception_detail::error_info_injector<boost::bad_lexical_cast> >'
  what():  bad lexical cast: source type value could not be interpreted as target
已放弃

搜索更多相关主题的帖子: std include catch cout int 
2022-10-24 11:05
rjsp
Rank: 20Rank: 20Rank: 20Rank: 20Rank: 20
等 级:版主
威 望:528
帖 子:9029
专家分:54050
注 册:2011-1-18
收藏
得分:20 
代码不全,但输出结果很正常呀,你捕获了 bootst::bad_lexical_cast 异常,然后又 throw 出去。外面没有捕获这个异常的代码,那按规定就应该直接调用 terminate()

你的代码逻辑我是看不懂
extern char *optarg;
unsigned tryfunc(char *a) // 这个 a 没有用到,不知道是干什么的
{
    int intnum; // 返回类型是 unsigned,你定义个 int,不知道有什么深意
    try {
        std::cout<<"ready to throw!\n";
        if(!(intnum=boost::lexical_cast<unsigned>(optarg))) // 这里可能抛出 bootst::bad_lexical_cast 异常
        throw (optarg); // 这里可能抛出 char* 异常
        #如果有错不传到catch // 从什么现象能看出“如果有错不传到catch”?
    }
    catch(...)
    {
        exception e; // 这个就看不懂了,你不捕获别人给你的异常,而是自己定义一个?
        std::cout << "-------------------wrong: " << e.what();
        throw; // 你throw的话,就应该保证有人catch,如果没有catch将调用terminate()
    }
    return intnum;
}


我写了个测试代码
程序代码:
#include <iostream>
#include <exception>
#include "boost/lexical_cast.hpp"

unsigned foo( const char* s )
{
    unsigned result = 0;

    try {
        result = boost::lexical_cast<unsigned>( s );
        if( result == 0 )
            throw s;
    }
    catch( const std::exception& e ) {
        std::cout << "-------------------wrong: " << e.what() << std::endl;
        throw;
    }
    catch( const char* s ) {
        std::cout << "-------------------wrong: " << s << std::endl;
        throw;
    }
    catch( ... ) {
        std::cout << "-------------------wrong: " << "other exception" << std::endl;
        throw;
    }

    return result;
}

using namespace std;

int main( void )
{
    try{ cout << foo("123") << endl; } catch(...){}
    try{ cout << foo("abc") << endl; } catch(...){}
    try{ cout << foo("0") << endl; } catch(...){}
    try{ cout << foo("nullptr") << endl; } catch(...){}
    try{ cout << foo("123abc") << endl; } catch(...){}
}

输出
123
-------------------wrong: bad lexical cast: source type value could not be interpreted as target
-------------------wrong: 0
-------------------wrong: bad lexical cast: source type value could not be interpreted as target
-------------------wrong: bad lexical cast: source type value could not be interpreted as target
2022-10-24 13:23
leshui
Rank: 2
等 级:论坛游民
威 望:1
帖 子:7
专家分:17
注 册:2022-10-24
收藏
得分:0 
#include <iostream>
#include <exception>
#include <string>
#include "boost/lexical_cast.hpp"
#include <unistd.h>
#include <cstdlib>
using namespace std;
extern char *optarg;
string help="use:./程序名 -r数字.\n";
unsigned tryfunc(char *s)
{
    unsigned result = 999;

    try {
       // result = boost::lexical_cast<unsigned>( s );
       // if( result == 999)
       if(!(result=boost::lexical_cast<unsigned>(s)))
            throw s;
    }
    catch( const std::exception& e ) {
        std::cout << "------exception &e-------------wrong: " << e.what() << std::endl;
        std::cout<<help<<endl;
        exit(EXIT_FAILURE);
    }
    catch( const char* s ) {
        std::cout << "------char *optarg-------------wrong: " << s << std::endl;
        std::cout<<help<<endl;
        exit(EXIT_FAILURE);
    }
    catch( ... ) {
        std::cout << "----- -------------wrong: " << "other exception" << std::endl;
        std::cout<<help<<endl;
       exit(EXIT_FAILURE);
    }

    return result;
}


int main(int argc,char *argv[])
{  
    int opt,rmint=5;
   
    while ((opt = getopt(argc, argv, ":r:")) != -1)
  {
   switch (opt)
     {
     case 'r':
     rmint=tryfunc(optarg);
     break;
     default: /* '?' */
     std::cout<<help<<endl;
     return 0;
      }
    }
    std::cout<<"you input optarg:"<<rmint<<endl;
 }
 

#谢谢楼上的人.我改进了一下,达到目的了.

[此贴子已经被作者于2022-10-24 20:19编辑过]

2022-10-24 19:58
leshui
Rank: 2
等 级:论坛游民
威 望:1
帖 子:7
专家分:17
注 册:2022-10-24
收藏
得分:0 
wei@wei:~/bin$ ./mn -r0
------char *optarg-------------wrong: 0
use:./程序名 -r数字.

wei@wei:~/bin$ ./mn -r1
you input optarg:1
wei@wei:~/bin$
#为啥0不能转?1都可以
2022-10-25 20:36
快速回复:关于try catch
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.022128 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved