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

Exception Handling Problem

P: n/a
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.

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


P: n/a
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.

Jul 23 '05 #2

P: n/a
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

Jul 23 '05 #3

P: n/a

BigBrian wrote:
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 -Brian


Jul 23 '05 #4

P: n/a
<co*******@gmail.com> wrote in message
BigBrian wrote:

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.


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?
Jul 23 '05 #5

P: n/a
Siemel Naran wrote:
<co*******@gmail.com> wrote in message
BigBrian wrote:
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.


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

Jul 23 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.