question on try/catch

X

xuatla

Hi,

I tried the following code about try/catch (from cplusplus.com)

// exceptions
#include <ciostream>
using namespace std;

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's expected to give "Exception: Out of range".
but i complied in g++ 3.3.3. and run it, the output is "Aborted".

Anyone help me to explain what's wrong here? or normal?

Thanks a lot!

xuatla
 
P

Phil Staite

xuatla said:
Hi,

I tried the following code about try/catch (from cplusplus.com)

// exceptions
#include <ciostream>
using namespace std;

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's expected to give "Exception: Out of range".
but i complied in g++ 3.3.3. and run it, the output is "Aborted".

Anyone help me to explain what's wrong here? or normal?

Try making the catch:

catch( const char* str )

I don't use g++ much, maybe it considers literal strings such as "Out of
range" to be const char*. Therefore it wouldn't match/catch a
non-const...
 
M

modemer

xuatla said:
Hi,

I tried the following code about try/catch (from cplusplus.com)

// exceptions
#include <ciostream>
using namespace std;

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)

should be:
catch (char const *str)
 
R

Rolf Magnus

xuatla said:
Hi,

I tried the following code about try/catch (from cplusplus.com)

// exceptions
#include <ciostream>
using namespace std;

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's expected to give "Exception: Out of range".
but i complied in g++ 3.3.3. and run it, the output is "Aborted".

Anyone help me to explain what's wrong here? or normal?

You're not catching the exception you threw. "Out of range" is of type
const char[13], which gets converted to const char* before being thrown. But
you're not catching const char*, and therefore the exception gets out. The
result is correct (std::abort() being called).
 
A

Andrew Koenig

#include <ciostream>
using namespace std;

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's expected to give "Exception: Out of range".
but i complied in g++ 3.3.3. and run it, the output is "Aborted".

Anyone help me to explain what's wrong here? or normal?

What happens if you change

catch (char * str)

to

catch (const char * str)

?
 

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,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top