抛出一个简单的例外 [英] Throwing a simple exception

查看:73
本文介绍了抛出一个简单的例外的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



假设你有一个非常简单的功能,如果它失败了,应该抛出一个

异常。但事情是,它并不重要,实际上

定义了一个异常类。 for,so ...是否有标准库中定义的一般例外类

我可以在这种情况下使用,

可能类似于:


命名空间std

{


class exception {};


}

void MyFunc()

{

//发生了一些不好的事情

抛出std :: exception();

}


-JKop


Let''s say you''ve a very simple function, that, if it fails, should throw an
exception. The thing is though, it''s not important enough to go and actually
define an "exception class" for, so... is there any general exception class
defined in the Standard Library that I could use in such circumstances,
maybe something like:

namespace std
{

class exception {};

}
void MyFunc()
{
//Something bad happens
throw std::exception();
}

-JKop

推荐答案



" JKop" < NU ** @ NULL.NULL>在消息中写道

news:%n ******************* @ news.indigo.ie ...

"JKop" <NU**@NULL.NULL> wrote in message
news:%n*******************@news.indigo.ie...

让我们说你有一个非常简单的功能,如果它失败了,应该抛出
异常。但问题是,实际上定义一个异常类并不重要。因为......在标准库中定义的任何一般例外
类我可以在这种情况下使用,
可能是这样的:

命名空间std
{class exception {};

}

void MyFunc()
{
/ /发生了一些不好的事情
抛出std :: exception();
}

Let''s say you''ve a very simple function, that, if it fails, should throw
an
exception. The thing is though, it''s not important enough to go and
actually
define an "exception class" for, so... is there any general exception
class
defined in the Standard Library that I could use in such circumstances,
maybe something like:

namespace std
{

class exception {};

}
void MyFunc()
{
//Something bad happens
throw std::exception();
}



std :: exception就是这样一个类,它定义于标题< exception> ;.

您还可以考虑< stdexcept>中的std :: runtime_error,它来自

std :: exception并允许您指定一个消息。


抛出std :: runtime_error(" wobbly wibble");


john



std::exception is just such a class, its defined in the header <exception>.
You could also consider std::runtime_error in <stdexcept>, it derives from
std::exception and allows you to specify a message.

throw std::runtime_error("wobbly wibble");

john


JKop写道:
让我们说你有一个非常简单的功能,如果它失败了,应该抛出一个
异常。但问题是,实际上定义一个异常类并不重要。 for,so ...在标准库中定义了一些我可以在这种情况下使用的常规异常类,
可能是这样的:
Let''s say you''ve a very simple function, that, if it fails, should throw an
exception. The thing is though, it''s not important enough to go and actually
define an "exception class" for, so... is there any general exception class
defined in the Standard Library that I could use in such circumstances,
maybe something like:




< exception>


定义std :: exception


< stdexecpt>


定义了一些从它派生的通用异常。



<exception>

defines std::exception

<stdexecpt>

defines a few general purpose exceptions derived from it.




JKop写道:


JKop wrote:
让我们说你有一个非常简单的功能,如果它失败了,应该抛出一个
异常。但问题是,实际上定义一个异常类并不重要。 for,so ...在标准库中定义的任何一般异常类我可以在这种情况下使用,
可能是这样的:

namespace std
{

班级例外{};

}




//

抛出std :: exception();
}
-JKop
Let''s say you''ve a very simple function, that, if it fails, should throw an
exception. The thing is though, it''s not important enough to go and actually
define an "exception class" for, so... is there any general exception class
defined in the Standard Library that I could use in such circumstances,
maybe something like:

namespace std
{

class exception {};

}

void MyFunc()
{
//Something bad happens
throw std::exception();
}

-JKop




例如:


#include< stdexcept>


使用命名空间std;


void myfunc(bool bad_things_happen)

{

if(bad_things_happen)

throw runtime_error(" my message here"); //使用runtime_error,因为

你不能将

//消息

与标准''exception''class <联系起来br $>
}

int main(int argc,char ** argv)

{

试试{

myfunc(true);

} catch(runtime_error& aa){//注意你可以在这里抓住''异常''如果你想要b / b
在stdexcept中捕获所有

//例外

cout<< aa.what(); //打印您的留言

}

}



for example:

#include <stdexcept>

using namespace std ;

void myfunc(bool bad_things_happen)
{
if (bad_things_happen)
throw runtime_error("my message here") ; // use a runtime_error since
you can''t associate a
//message
with the standard ''exception'' class
}
int main(int argc, char **argv)
{
try {
myfunc(true) ;
} catch (runtime_error &aa) { // note you could catch ''exception'' here if you
want to catch all the
// exceptions in stdexcept
cout << aa.what() ; // prints your message
}
}


这篇关于抛出一个简单的例外的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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