[英]C++ : Throw Exception weird behavior?
I just noticed something, and I think it's pretty weird. 我刚刚发现了一些东西,我觉得这很奇怪。 It's not very important, but it draws my curiosity.
这不是很重要,但是却引起了我的好奇心。
Imagine you declare a class: 假设您声明一个类:
class myException : public std::exception
{
/*do stuff*/
} myExep;
I just noticed that whenever you throw that exception, you need to do it differently depending on whether you use myException
or myExep
: 我只是注意到,无论何时抛出该异常,都需要根据使用
myException
还是myExep
:
try
{
if (/*whatever*/)
throw myException();
}
or : 要么 :
try
{
if (/*whatever*/)
throw myExep;
}
I do not understand why you need ()
in one case and not the other. 我不明白为什么在一种情况下而不是在另一种情况下需要
()
。 I use clang++ as compiler, dunno if it has anything to do with it. 我将clang ++用作编译器,如果与之无关,则不知道。 I use
-Wall -Werror -Wextra
flags. 我使用
-Wall -Werror -Wextra
标志。
It's not really a big deal, I just want to understand what really happens. 这并不是什么大不了的事,我只是想了解真正发生的事情。
class Foo {
...
} bar;
is just a shorter way of writing 只是一种较短的写作方式
class Foo {
...
};
Foo bar;
You can also use this syntax without giving the type a name: 您也可以使用以下语法,而无需为类型指定名称:
struct {
int x, y;
} p;
// p is an object with p.x and p.y fields
In your example, myException
is a type while myExp
is a (presumably global) object. 在您的示例中,
myException
是类型,而myExp
是(可能是全局)对象。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.