By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,797 Members | 1,848 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,797 IT Pros & Developers. It's quick & easy.

try and throw question

P: n/a
when I run the following code,it just generate "aborted",but the
anticipated answer is
"Caught \"char *\" exception" ,it seems to be no run the 'catch'
sentence.
please help!
#include <iostream>
using namespace std;
class CanThrowException
{
int *array;
public:
CanThrowException(int array_size) throw (char *) {
// if ((array = (int *)malloc(sizeof(int) * array_size)) == 0)
/* If allocation fails */
throw "Memory allocation failed";
}

//... Other stuff

};

int
main()
{
CanThrowException *ins;

try { /* Try to construct object */

ins = new CanThrowException(10);
}

catch (char *) { /* Catch any exception thrown by constructor */
cerr << "Caught \"char *\" exception" << endl;

}
// catch (...) { /* If constructor can throw anything else */
// cerr << "Caught unknown exception" << endl;
// }

}

Jul 23 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
* lixiaoyao:
when I run the following code,it just generate "aborted",but the
anticipated answer is
"Caught \"char *\" exception" ,it seems to be no run the 'catch'
sentence.
The anticipation is incorrect.

please help!
#include <iostream>
using namespace std;
class CanThrowException
{
int *array;
Better use a std::vector<int>.
public:
CanThrowException(int array_size) throw (char *) {
Don't use throw specifications, except "throw()".. Although in this case it
captures a bug it leads to unmaintainable code. C++ isn't Java.

// if ((array = (int *)malloc(sizeof(int) * array_size)) == 0)
/* If allocation fails */
throw "Memory allocation failed";
Here you throw a "char const*".

That isn't what's specificed, so you end up with a call to std::terminate
(for a standard-conforming compiler).

Use standard exceptions such as std::runtime_error.

}

//... Other stuff

};

int
main()
{
CanThrowException *ins;


Don't use raw pointers.

Here: at least use a std::auto_ptr.

Better: don't use a pointer unless necessary, and it isn't here.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2

P: n/a
* lixiaoyao:

// if ((array = (int *)malloc(sizeof(int) * array_size)) == 0)


Don't use 'malloc' (use C++ 'new' instead).

Don't use C-style casts.

Don't use raw pointers (here: use a std::vector<int> instead).

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #3

P: n/a
I am sorry,but can you correct the code for me?
Thanks

Jul 23 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.