简体   繁体   中英

throw syntax in exception c++

Maybe that's a stupid question, I'm new to C++:

I read a little about the exception mechanism, and in some code I encountered the line:

throw SomeClass();

what does it mean? Is it a call to the class c'tor and then an object of that class is thrown?

In other examples I saw it was always that "throw" threw an instance (specific string or int, and in here I'm confused because I know c'tor doesn't have return type.

I don't understand the "logic" behind this expression...

Yes.

More specifically, this constructs a temporary and throws it.

First, you should throw some instance derived from std::exception (Although you might throw integers, c-strings, ..., which is usually bad). In some cases you might throw a special exception (std:bad_alloc is one example).

Then, you always throw a temporary instance (which has to be constructed) by invocation of a constructor: throw SomeClass();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM