| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"Ravi I Singh" |
| Date: |
13 Oct 2003 02:58:00 AM |
| Object: |
Problems with exception handling in g++ version 2.95.4 [FreeBSD] |
--------------05829806C14A19A4380C3F68
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: 7bit
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
--
Ravi I Singh
e-mail:
--------------05829806C14A19A4380C3F68
Content-Type: text/html; charset=us-ascii
Content-Transfer-Encoding: 7bit
<!doctype html public "-//w3c//dtd html 4.0 transitional//en">
<html>
Hello,
<p> I tried to compile the following program from a C++ tutorial
web page:
<br>
<p>// exceptions
<br> #include <iostream.h>
<p>int main () {
<br> char myarray[10];
<br> try
<br> {
<br> for (int n=0; n<=10; n++)
<br> {
<br> if (n>9) throw "Out of range";
<br> myarray[n]='z';
<br> }
<br> }
<br> catch (char * str)
<br> {
<br> cout << "Exception: " << str <<
endl;
<br> }
<br> return 0;
<br>}
<p> It core dumps when I try to run it. Any ideas? Thanks in
advance.
<br>
<p>Ravi
<pre>--
Ravi I Singh
e-mail:
</pre>
</html>
--------------05829806C14A19A4380C3F68--
.
|
|
| User: "Rolf Magnus" |
|
| Title: Re: Problems with exception handling in g++ version 2.95.4 [FreeBSD] |
13 Oct 2003 04:17:16 AM |
|
|
Ravi I Singh wrote:
Hello,
I tried to compile the following program from a C++ tutorial web
page:
// exceptions
#include <iostream.h>
That header is non-standard. Better use <iostream> instead. cout and
endl will then be in the 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 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*.
.
|
|
|
|

|
Related Articles |
|
|