关于异常说明的问题

insulted 2009-02-23 10:04:41
今天开翻《C++ STL》一书,就发现一个问题,那就是目前的编译器好像尚不支持C++的异常说明,源代码如下:

#include "stdafx.h"
#include <iostream>
#include <string>

using namespace std;

class c1{
public:
c1(){
c1str = "error 1";
}
string c1str;
};

class c2{
public:
c2(){
c2str = "error 2";
}
string c2str;
};

void fn() throw (c1)
{
cout<<"have problem"<<endl;
throw c2();
}

int main()
{
try{
fn();
}

catch (const c2& err)
{
cout << err.c2str<<endl;
}

cout <<"Done"<<endl;
return 0;
}


在VS 2005中异常说明会得出警告: warning C4290: 忽略C++ 异常规范,但指示函数不是__declspec(nothrow)

警告MSDN中的原文解释如下:

警告消息
忽略 C++ 异常规范,但指示函数不是 __declspec(nothrow)

使用异常规范声明函数,Visual C++ 接受但并不实现此规范。包含在编译期间被忽略的异常规范的代码可能需要重新编译和链接,以便在支持异常规范的未来版本中重用。

有关更多信息,请参见 Exception Specifications。

使用 warning 杂注可避免出现此警告:

意思大概就是异常说明VS 2005仅仅是接受,而并没有实现,也就是相当于没有这个功能,但是不报错。此源代码在Dev 4.9.9.0可以得出正确结果。提示程序在运行时被非正常终结,并且仅仅只输出have problem;而VS 2005把所有的东西都输出了。据说VS 2005已经非常符合标准了,就我的学习过程的确是这样,今天看来,还尚有为尽之处,不过,但是也没有人说它完全符合标准,好像也就97%多吧。

将此事进一步研究,发现如下代码:

#include <iostream>
#include <string>
using namespace std;

class c1{
public:
c1(){
c1str = "error 1";
}
string c1str;
};

class c2{
public:
c2(){
c2str = "error 2";
}
string c2str;
};

void fn() throw (c1,bad_exception)
{
cout<<"have problem"<<endl;

throw c2();
}

int main()
{
try{
fn();
}
catch (const c2 &err)
{
cout << err.c2str <<endl;
}
catch (const bad_except ion&)
{
cout << "catch bad_exception"<<endl;
}
catch (...)
{
cout << "catch another exception"<<endl;
}
cout <<"Done"<<endl;

return 0;
}


当然在VS中还是只能捕捉到异常c2,而在DEV中奇怪的是确还是中断了,按《C++ STL》书中的说法,此时应该捕捉到异常bad_exception才对,另外,有一点想说的是,《C++ Primer》一书第4版中我没有找到关于bad_exception的介绍,而其它类型的异常倒还是有。再另外,我在VS 2005的exception头文件的源代码中的确发现了bad_exception异常,其注释为base of all bad exceptions。在DEV中源代码注释为f an %exception is thrown which is not listed in a function's %exception specification, one of these may be thrown.和This declaration is not useless,如此,就不是异常的捕捉代码有问题,而是的确程序没有正常抛出了。则我,书,和DEV三者中必有一错,希望高人指出。
...全文
393 17 打赏 收藏 转发到动态 举报
写回复
用AI写文章
17 条回复
切换为时间正序
请发表友善的回复…
发表回复
cs60089 2009-03-14
  • 打赏
  • 举报
回复
这都让我看到了,还是一个月前的,看来C++才是csdn最活跃的地方啊!
Chevin 2009-02-24
  • 打赏
  • 举报
回复
mark and up
waizqfor 2009-02-24
  • 打赏
  • 举报
回复
帮顶一下 !~
DarknessTM 2009-02-24
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 taodm 的回复:]
这就晕啦?你要看过boost库的源码,看看它怎么颠来倒去处理各编译器不同,就知道还是学java、C#算了。
[/Quote]

其实事实上 JAVA的编译器基本就一个sun出的(我想其他的也应该有,但是应用的很少吧)
C#就ms自己出

这样也就没所谓编译器间的区别了吧
bfhtian 2009-02-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 taodm 的回复:]
有标准也可以不依,编译器可以自行作出自认为对用户最合理的扩展。
书没错,编译器也谈不上错。
错的是你,你不该认为一定要有规则、一定要有规律。
C++程序员只能强行记住每一特性在不同编译器下反应
[/Quote]
有点玄乎
taodm 2009-02-24
  • 打赏
  • 举报
回复
这就晕啦?你要看过boost库的源码,看看它怎么颠来倒去处理各编译器不同,就知道还是学java、C#算了。
lgccaa 2009-02-24
  • 打赏
  • 举报
回复
up
fallening 2009-02-24
  • 打赏
  • 举报
回复
[Quote=引用 8 楼 taodm 的回复:]
有标准也可以不依,编译器可以自行作出自认为对用户最合理的扩展。
书没错,编译器也谈不上错。
错的是你,你不该认为一定要有规则、一定要有规律。
C++程序员只能强行记住每一特性在不同编译器下反应
[/Quote]
我晕倒,这个…………
taodm 2009-02-24
  • 打赏
  • 举报
回复
有标准也可以不依,编译器可以自行作出自认为对用户最合理的扩展。
书没错,编译器也谈不上错。
错的是你,你不该认为一定要有规则、一定要有规律。
C++程序员只能强行记住每一特性在不同编译器下反应
nineforever 2009-02-24
  • 打赏
  • 举报
回复
如MSDN所说,VC没有实现exception specification

通常的exception specification会导致额外的开销。编译器需要隐式的加上try+catch,比如
void f() throw (std::bad_alloc)
{
//...
}

大致等价于
void f() throw (std::bad_alloc)
{
try {
//...
} catch (std::bad_alloc &) {
throw;
} catch (...) {
std::unexpected();
}
}


因为根据标准
an exception-specification guarantees that only the listed exceptions will be thrown.
同时
If the exception specification
includes the type std::bad_exception then any exception not on the list may be replaced by
std::bad_exception within the function std::unexpected()
majun01 2009-02-24
  • 打赏
  • 举报
回复
mark
hemiya 2009-02-24
  • 打赏
  • 举报
回复
[Quote=引用 1 楼 Waiting4you 的回复:]
如果我没记错的话,声明void fn() throw (c1){...}的意思是fn函数只可能抛出c1类型的异常.而你在代码里却抛出了一个c2类型?!!!
[/Quote]
我记得好像是异常的声明不是保证,只是给人看得,编译器不负责检查.
herman~~ 2009-02-24
  • 打赏
  • 举报
回复
编译器并不是对标准一成不变的实现
fallening 2009-02-24
  • 打赏
  • 举报
回复
[Quote=引用 11 楼 taodm 的回复:]
这就晕啦?你要看过boost库的源码,看看它怎么颠来倒去处理各编译器不同,就知道还是学java、C#算了。
[/Quote]
看到了,以前从来没有正眼看过这个文件的
cat /usr/include/boost/regex/config.hpp

/*
*
* Copyright (c) 1998-2002
* John Maddock
*
* Use, modification and distribution are subject to the
* Boost Software License, Version 1.0. (See accompanying file
* LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
*
*/

/*
* LOCATION: see http://www.boost.org for most recent version.
* FILE config.hpp
* VERSION see <boost/version.hpp>
* DESCRIPTION: regex extended config setup.
*/

#ifndef BOOST_REGEX_CONFIG_HPP
#define BOOST_REGEX_CONFIG_HPP
/*
* Borland C++ Fix/error check
* this has to go *before* we include any std lib headers:
*/
#if defined(__BORLANDC__)
# include <boost/regex/config/borland.hpp>
#endif

/*****************************************************************************
*
* Include all the headers we need here:
*
****************************************************************************/

#ifdef __cplusplus

# ifndef BOOST_REGEX_USER_CONFIG
# define BOOST_REGEX_USER_CONFIG <boost/regex/user.hpp>
# endif

# include BOOST_REGEX_USER_CONFIG

# include <boost/config.hpp>

#else
/*
* C build,
* don't include <boost/config.hpp> because that may
* do C++ specific things in future...
*/
# include <stdlib.h>
# include <stddef.h>
# ifdef _MSC_VER
# define BOOST_MSVC _MSC_VER
# endif
#endif

/*****************************************************************************
*
* Boilerplate regex config options:
*
****************************************************************************/

/* Obsolete macro, use BOOST_VERSION instead: */
#define BOOST_RE_VERSION 320

/* fix: */
#if defined(_UNICODE) && !defined(UNICODE)
#define UNICODE
#endif

/*
* Fix for gcc prior to 3.4: std::ctype<wchar_t> doesn't allow
* masks to be combined, for example:
* std::use_facet<std::ctype<wchar_t> >.is(std::ctype_base::lower|std::ctype_base::upper, L'a');
* returns *false*.
*/
#ifdef __GLIBCPP__
# define BOOST_REGEX_BUGGY_CTYPE_FACET
#endif

/*
* Intel C++ before 8.0 ends up with unresolved externals unless we turn off
* extern template support:
*/
#if defined(BOOST_INTEL) && defined(__cplusplus) && (BOOST_INTEL <= 800)
# define BOOST_REGEX_NO_EXTERNAL_TEMPLATES
#endif
/*
* Visual C++ doesn't support external templates with C++ extensions turned off:
*/
#if defined(_MSC_VER) && !defined(_MSC_EXTENSIONS)
# define BOOST_REGEX_NO_EXTERNAL_TEMPLATES
#endif

/*
* If there isn't good enough wide character support then there will
* be no wide character regular expressions:
*/
#if (defined(BOOST_NO_CWCHAR) || defined(BOOST_NO_CWCTYPE) || defined(BOOST_NO_STD_WSTRING))
# if !defined(BOOST_NO_WREGEX)
# define BOOST_NO_WREGEX
# endif
#else
# if defined(__sgi) && (defined(__SGI_STL_PORT) || defined(_STLPORT_VERSION))
/* STLPort on IRIX is misconfigured: <cwctype> does not compile
* as a temporary fix include <wctype.h> instead and prevent inclusion
* of STLPort version of <cwctype> */
# include <wctype.h>
# define __STLPORT_CWCTYPE
# define _STLP_CWCTYPE
# endif

#ifdef __cplusplus
# include <boost/regex/config/cwchar.hpp>
#endif

#endif

/*
* If Win32 support has been disabled for boost in general, then
* it is for regex in particular:
*/
#if defined(BOOST_DISABLE_WIN32) && !defined(BOOST_REGEX_NO_W32)
# define BOOST_REGEX_NO_W32
#endif

/* disable our own file-iterators and mapfiles if we can't
* support them: */
#if !defined(BOOST_HAS_DIRENT_H) && !(defined(_WIN32) && !defined(BOOST_REGEX_NO_W32))
# define BOOST_REGEX_NO_FILEITER
#endif

/* backwards compatibitity: */
#if defined(BOOST_RE_NO_LIB)
# define BOOST_REGEX_NO_LIB
#endif

#if defined(__GNUC__) && (defined(_WIN32) || defined(__CYGWIN__))
/* gcc on win32 has problems if you include <windows.h>
(sporadically generates bad code). */
# define BOOST_REGEX_NO_W32
#endif
#if defined(__COMO__) && !defined(BOOST_REGEX_NO_W32) && !defined(_MSC_EXTENSIONS)
# define BOOST_REGEX_NO_W32
#endif

/*****************************************************************************
*
* Wide character workarounds:
*
****************************************************************************/

/*
* define BOOST_REGEX_HAS_OTHER_WCHAR_T when wchar_t is a native type, but the users
* code may be built with wchar_t as unsigned short: basically when we're building
* with MSVC and the /Zc:wchar_t option we place some extra unsigned short versions
* of the non-inline functions in the library, so that users can still link to the lib,
* irrespective of whether their own code is built with /Zc:wchar_t.
*/
#if defined(__cplusplus) && (defined(BOOST_MSVC) || defined(__ICL)) && !defined(BOOST_NO_INTRINSIC_WCHAR_T) && defined(BOOST_WINDOWS) && !defined(__SGI_STL_PORT) && !defined(_STLPORT_VERSION) && !defined(BOOST_RWSTD_VER)
# define BOOST_REGEX_HAS_OTHER_WCHAR_T
# ifdef BOOST_MSVC
# pragma warning(push)
# pragma warning(disable : 4251 4231 4660)
# endif
# ifdef _DLL
# include <string>
extern template class __declspec(dllimport) std::basic_string<unsigned short>;
# endif
# ifdef BOOST_MSVC
# pragma warning(pop)
# endif
#endif


/*****************************************************************************
*
* Set up dll import/export options:
*
****************************************************************************/

#if defined(BOOST_HAS_DECLSPEC) && (defined(BOOST_REGEX_DYN_LINK) || defined(BOOST_ALL_DYN_LINK)) && !defined(BOOST_REGEX_STATIC_LINK)
# if defined(BOOST_REGEX_SOURCE)
# define BOOST_REGEX_DECL __declspec(dllexport)
# define BOOST_REGEX_BUILD_DLL
# else
# define BOOST_REGEX_DECL __declspec(dllimport)
# endif
#endif

#ifndef BOOST_REGEX_DECL
# define BOOST_REGEX_DECL
#endif

#if !defined(BOOST_REGEX_NO_LIB) && !defined(BOOST_REGEX_SOURCE) && !defined(BOOST_ALL_NO_LIB) && defined(__cplusplus)
# define BOOST_LIB_NAME boost_regex
# if defined(BOOST_REGEX_DYN_LINK) || defined(BOOST_ALL_DYN_LINK)
# define BOOST_DYN_LINK
# endif
# ifdef BOOST_REGEX_DIAG
# define BOOST_LIB_DIAGNOSTIC
# endif
# include <boost/config/auto_link.hpp>
#endif

/*****************************************************************************
.............................
  • 打赏
  • 举报
回复
关注,学习。
ltc_mouse 2009-02-23
  • 打赏
  • 举报
回复
关注

等taodm~
Waiting4you 2009-02-23
  • 打赏
  • 举报
回复
如果我没记错的话,声明void fn() throw (c1){...}的意思是fn函数只可能抛出c1类型的异常.而你在代码里却抛出了一个c2类型?!!!

65,211

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧