hey!
I have some questions regarding exceptions.
concerning this code:

try
15     {
16        cout << "  Function throwException throws an exception\n";
17        throw exception(); // generate exception
18     } // end try
19     catch ( exception & ) // handle exception
20     {
21        cout << "  Exception handled in function throwException"
22           << "\n  Function throwException rethrows exception";
23        throw; // rethrow exception for further processing
24     } // end catch

the book states that in line 17 exception is an instance of class exception, that I can't understand, it looks to me more like a function, I would write it that way "throw exception exp" as an example.
Thanks.

Recommended Answers

All 4 Replies

>> it looks to me more like a function

Or like the construction of an object, which it is.

If I have a class named "my_class", I can create an object of that class with "my_class()", which means the object is default-constructed and doesn't have a name (a temporary). So, the "exception()" just creates a temporary object of class "exception" that is default-constructed. The "throw" keyword takes whatever you give it (e.g. object) and propagates it (or carries it) back to the first try-block it encounters, where it will attempt to match it to one of the catch-statements.

You need to provide the throw statement with something to throw, it is most usual to give it a temporary object since there is usually no point in creating the exception object before throwing it, but in theory you could also do:

try
{
   cout << "  Function throwException throws an exception\n";
   exception e; //create exception object.
   throw e;     // and throw it.
} // end try
catch ( exception & ) // handle exception
{
   cout << "  Exception handled in function throwException"
        << "\n  Function throwException rethrows exception";
   throw; // rethrow exception for further processing
} // end catch

But why take two lines of code to do what one lines does equally well:

throw exception(); //this is pretty much the same as above (but less wasteful).

It's the first time to be exposed to something like that, actually that is the first time to know that I can invoke the constructor directly by this way.
it seems an interesting topic for me and I want to know more about it, when to use such method and so on, could you please tell where can I read more about this topic?

Usually, I refer beginners to Marshall Cline's C++ FAQ page for general explanation of just about everything that is fundamental to C++.

>>actually that is the first time to know that I can invoke the constructor directly by this way.

It is not a direct invocation of the constructor. It is creating a temporary object, that's not the same thing. A constructor cannot be invoked directly (as a function call), it is always associated with the creation of an object (that's why it is called a "constructor", because it is used during the construction of an object).

I presume you are aware of this syntax:

int a(42);  //creates a local variable, of type 'int', called 'a', and initialized to '42'.

Now, if you just want to create a local variable but that is temporary (which means it does not need to have a name), than you just omit the name from the syntax, and get:

int(42);

Now, if you have a class that takes no parameters to one of its constructors (the default constructor), then the equivalent to the above for that custom class with default initialization is:

my_class();

That's about all there is to know about this. Don't know what else to say.

>>it seems an interesting topic for me

I can't be sure what "interesting topic" you are talking about. If you are talking about the above matter about creating temporary objects, then I can hardly see how you would find it "an interesting topic", it's just a basic syntax rule of C++. If you are talking about exception handling, then that is certainly a very vast and interesting subject. This FAQ section is one of the best resources I know about exceptions and error handling (many authors tend to give huge emphasis on OOP and standard libraries, they often tend to neglect the subject of exception-handling, partly because it is harder and less clear-cut than the other subjects). Just google for "exception-handling in C++" and you will find plenty of interesting articles.

Thanks. I was confused a little bit, I thought it is a calling to constructor directly, but now I got it. thanks again for the further explanation.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.