Day_Dreamer
Day_Dreamer

Reputation: 3373

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...

Upvotes: 0

Views: 142

Answers (2)

user2249683
user2249683

Reputation:

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();

Upvotes: 0

Quentin
Quentin

Reputation: 63154

Yes.

More specifically, this constructs a temporary and throws it.

Upvotes: 1

Related Questions