Problems with exception handling in g++ version 2.95.4 [FreeBSD]

R

Ravi I Singh

Hello,

I tried to compile the following program from a C++ tutorial web
page:


// exceptions
#include <iostream.h>

int main () {
char myarray[10];
try
{
for (int n=0; n<=10; n++)
{
if (n>9) throw "Out of range";
myarray[n]='z';
}
}
catch (char * str)
{
cout << "Exception: " << str << endl;
}
return 0;
}

It core dumps when I try to run it. Any ideas? Thanks in advance.


Ravi
 
R

Rolf Magnus

Ravi said:
Hello,

I tried to compile the following program from a C++ tutorial web
page:


// exceptions
#include <iostream.h>


int main () {
char myarray[10];
try
{
for (int n=0; n<=10; n++)
{
if (n>9) throw "Out of range";
myarray[n]='z';
}
}
catch (char * str)
{
cout << "Exception: " << str << endl;
}
return 0;
}

It core dumps when I try to run it. Any ideas?

Because the thrown exception is not caught, which results in a call to
std::terminate(), which in turn calls std::abort(). And that function
dumps core on most un*x systems. The problem is that you throw a const
char*, but catch a char*.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top