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



 DEVELOP > c-Plus-Plus > Problems with exception handling in g++ version 2.95.4 [FreeBSD]

LINK TO THIS PAGE  


rating :  0   |  0


  Page 1 of 1

1

 
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>&nbsp;&nbsp; I tried to compile the following program from a C++ tutorial
web page:
<br>&nbsp;
<p>// exceptions
<br>&nbsp; #include &lt;iostream.h>
<p>int main () {
<br>&nbsp; char myarray[10];
<br>&nbsp; try
<br>&nbsp; {
<br>&nbsp;&nbsp;&nbsp; for (int n=0; n&lt;=10; n++)
<br>&nbsp;&nbsp;&nbsp; {
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; if (n>9) throw "Out of range";
<br>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; myarray[n]='z';
<br>&nbsp;&nbsp;&nbsp; }
<br>&nbsp; }
<br>&nbsp; catch (char * str)
<br>&nbsp; {
<br>&nbsp;&nbsp;&nbsp; cout &lt;&lt; "Exception: " &lt;&lt; str &lt;&lt;
endl;
<br>&nbsp; }
<br>&nbsp; return 0;
<br>}
<p>&nbsp; It core dumps when I try to run it. Any ideas?&nbsp;Thanks in
advance.
<br>&nbsp;
<p>Ravi
<pre>--
Ravi I Singh
e-mail:

</pre>
&nbsp;</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*.
.


  Page 1 of 1

1

 


Related Articles
 

NEWER

pg.1232     pg.940     pg.716     pg.544     pg.412     pg.311     pg.234     pg.175     pg.130     pg.96     pg.70     pg.50     pg.35     pg.24     pg.16     pg.10     pg.6     pg.3     pg.1

OLDER