Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 07-15-2004, 08:24 AM   #1
Registered: May 2004
Posts: 130

Rep: Reputation: 15
help me find a BUG in a little C++ program

This is my source of program

[root@localhost mycpp]# ls
dive.cpp dive.h glibc.c linuxsoc main.cpp try1.cpp try2.cpp
[root@localhost mycpp]# cat try1.cpp
#include <iostream.h>

int main () {
char myarray[10];
for (int n=0; n<=10; n++)
if (n>9) throw "Out of range";
catch (char * str)
cout << "Exception: " << str << endl;
return 0;
[root@localhost mycpp]#

so I compile it

[root@localhost mycpp]# make try1
g++ try1.cpp -o try1
In file included from /usr/include/c++/3.2.2/backward/iostream.h:31,
from try1.cpp:1:
/usr/include/c++/3.2.2/backward/backward_warning.h:32:2: warning: #warning This file includes at least one deprecated or antiquated header. Please consider using one of the 32 headers found in section of the C++ standard. Examples include substituting the <X> header for the <X.h> header for C++ includes, or <sstream> instead of the deprecated header <strstream.h>. To disable this warning use -Wno-deprecated.
[root@localhost mycpp]#

and run the program so this is my result ------------->>

[root@localhost mycpp]# ./try1
[root@localhost mycpp]#

what's up??
I think the output must be "Exception: Out of range"

Old 07-15-2004, 09:02 AM   #2
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
catch (const char *str)

and <iostream.h>

changes to <iostream> and using namespace std; to get rid of the warnings.

Last edited by dakensta; 07-15-2004 at 09:04 AM.
Old 07-15-2004, 09:03 AM   #3
Senior Member
Registered: Apr 2003
Location: Lancaster, England
Distribution: Debian Etch, OS X 10.4
Posts: 1,263

Rep: Reputation: 51
the problem is your throwing "Out of range" which is a const char * which is not castable to a char * hence why its not being caught. you can fix this by either catching const char * or by throwing something castable to a char *
Old 07-15-2004, 09:35 AM   #4
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
Or even catch (...)

kev's comment reminds me of a nice article about exception handling and casting, which, although not immediately relevent here might be of interest to anyone reading the thread:



Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Please help find bug in my c++ program! twirl Programming 2 09-16-2005 12:06 AM
cant find program Circuit Monkey Linux - Newbie 3 03-25-2005 05:36 AM
help me find a few bug when compile program vanhelsing Programming 10 07-28-2004 04:01 PM
couldn't find program ar$e munkie_poo Mandriva 3 05-24-2004 02:41 PM
Can 't find the bug Linh Programming 2 07-23-2003 04:53 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:01 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration