也来说说抛出异常

0 views
Skip to first unread message

邱戈川

unread,
Mar 10, 2006, 4:27:23 AM3/10/06
to 基于ACE和SpiderMonkey的SMS虚拟运营系统
如果函数可能抛出异常,则最好在函数定义的时候指明异常类型。
如:
class A
{
enum {ERROR1, ERROR2};

public:
A(){}
void f( const int& p) throw (B, D);
void f( int& p) throw (...){};

};


注意:
1、对于VC6还没有支持throw (...)这种写法。VC7已经支持
2、对于throw
括号内的类型必须是类,虽然在函数中可以抛出enum的异常,但是这里不能写成enum的类型。
3、VC6,VC7编译器实际上是忽略这种写法的(即跟没有写一样),如果想忽略这个警告可以用:#pragma
warning( disable : 4290 )

时代过客

unread,
Mar 12, 2006, 9:22:14 PM3/12/06
to 基于ACE和SpiderMonkey的SMS虚拟运营系统
对于throw 括号内的类型必须是类
============================
不一定要是类的,一些基本类型也可以,如int

candid Qiu

unread,
Mar 12, 2006, 9:31:18 PM3/12/06
to ACE...@googlegroups.com
这一点我用vc7和vc6都不可以,大家可以试试。

时代过客

unread,
Mar 12, 2006, 10:01:53 PM3/12/06
to 基于ACE和SpiderMonkey的SMS虚拟运营系统
class A
{
public:
void fun() throw(int) {throw 1;};
};
void main(void)
{
try
{
A a ;
a.fun() ;
}
catch (int e)
{
cout << e << endl ;
}
}

输出: 1

将:void fun() throw(int) {throw 1;};
改为:void fun() throw(int) {throw 1f;}; //
编译不通过,可见int类型被强制了

Reply all
Reply to author
Forward
0 new messages