473,385 Members | 1,357 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

try and throw question

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
3 1434
* 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
* 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
I am sorry,but can you correct the code for me?
Thanks

Jul 23 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: Lasse Vågsæther Karlsen | last post by:
From the book by Jeffrey Richter: throw; will not change the origin of the exception, whereas throw ex; will change the origin of the exception to this statement. When I try the following...
2
by: Kerri | last post by:
Hi, I am new to .NET In my Error Logic on my Aspx pages when an error happens it hits my catch statement where I throw an Exception. My question is : what is the difference between Thwo...
4
by: James Radke | last post by:
Hello, I am attempting to use the proper Try/Catch technique when accessing my Microsoft SQL server database and have a question... If I use something similar to the following: Try set up...
13
by: Jacek Dziedzic | last post by:
Hi! <OT, background> I am in a situation where I use two compilers from different vendors to compile my program. It seems that recently, due to a misconfiguration, library conflict or my...
7
by: > Adrian | last post by:
If I code as follows try { \\ do something } catch (.....Exception) { throw; }
1
by: Ralph Moritz | last post by:
Hi, I've written a class called ConfigParser, which can be used to parse config files. (Surprise!) I have overloaded the shift operators to mimic the std iostreams. My question is, should...
8
by: Grizlyk | last post by:
Hello I want to understand the exception habdling in C++ more, because i think no one can apply anything free (free - without remembering large unclear list of rules and exceptions from rules...
18
by: Ranganath | last post by:
Why is throw keyword considered as an operator?
6
by: jason.cipriani | last post by:
Consider this program, which defines a template class who's template parameter is the type of an exception that can be thrown by members of the class: === BEGIN EXAMPLE === #include...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.