Exception Handling Problem

C

codefixer

Hi,

Whenever an exception is raised, I ma getting just "Abort" and not the
customized message.

Even the simplest program ,like this is aborting...
#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;
}

I am running on g++ 3.2.3 on solaris.
I did try to print the values inside loop and it does print till 10 and
then Aborts.

Any idea why this is happening ?

Thanks.
 
C

codefixer

Adding more. In the debugger I see
Program received signal SIGABRT, Aborted.
0xff21fc04 in _libc_kill () from /lib/libc.so.1

And why would the program receive the same ?? Any hints ?

Thanks.
 
B

BigBrian

I'm not at a machine with a compiler, so I can't verify this, but "Out
of range" is a const char *, which you aren't catching. You need to
change your catch.

-Brian
 
C

codefixer

BigBrian said:
I'm not at a machine with a compiler, so I can't verify this, but "Out
of range" is a const char *, which you aren't catching. You need to
change your catch.
Yes changing that to const char * worked.
Thanks
 
S

Siemel Naran

BigBrian wrote:
Yes changing that to const char * worked.

Not sure if it is standard across platforms. I think that in builtin C
strings have type char*, and C++ might inherit this rule for historical
reasons. Making builtin strings have type const char* might be an
extension.

So what I'm saying is that if you compile on other compilers, they might
catch char*, but const char* would go unhandled. On gnu, try compiling
with --pedantic to enforce the ANSI rules, and see what happens. Though to
be sure, you should also compile on other platforms.

Why not throw a std::runtime_error or some class derived from
std::exception?
 
K

Kristo

Siemel said:
Not sure if it is standard across platforms. I think that in builtin C
strings have type char*, and C++ might inherit this rule for historical
reasons. Making builtin strings have type const char* might be an
extension.

In my copy of the standard, 2.13.4.1 states that a string literal has
type array of const char. So, catching a const char * is the correct
way to go on all conforming implementations.
So what I'm saying is that if you compile on other compilers, they might
catch char*, but const char* would go unhandled.

This is false as noted above.
On gnu, try compiling with --pedantic to enforce the ANSI rules, and
see what happens. Though to be sure, you should also compile on other
platforms.

<OT>
The GNU C++ compiler option --pedantic is not a panacea for determining
whether or not your code meets the language standard. It only issues
diagnostics for stuff that the standard specifically says it has to,
and IIRC it doesn't do so in every case.
</OT>

[snip good advice about throwing a std::exception]

Kristo
 

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

Forum statistics

Threads
474,436
Messages
2,571,696
Members
48,796
Latest member
Greg L.
Top