Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old Oct 29th, 2010, 10:01 AM   #1
Patfish
Newbie
 
Patfish's Avatar
 
Join Date: Oct 2010
Posts: 8
Rep Power: 0 Patfish is on a distinguished road
Talking C++ Exception Handling

Hello everyone,

i'm trying to get this program, it searches for the final 'a' in an word inputted by the user, to terminate normally with a predefined error message ("This word is too long") if a word longer than 20 chars is entered. I've used a try throw catch statement and I seem to be able to get the program to terminate but I cant get the error message to appear. Can someone help me out with where i'm going wrong please? code and output below
/* 
   
#include <iostream>
#include <cstdlib>

using namespace std;

int main( )
{
  const int WORD_LENGTH   = 20 ;
  const int INVALID_INDEX = -1 ;

  const char NULL_CHAR   = '\0' ;
  const char SEARCH_CHAR = 'a' ;

  char word[ WORD_LENGTH ] ;   

   char* word_ptr     = word ;
   int   indexOf_char = INVALID_INDEX ;

try
  {
   cout << "Input a word: " ;
   cin  >> word ;
   
   if  (word[ WORD_LENGTH] > 20 )
     { 
       throw "Incorrect Input - Word Too Long";
     }
  }

 catch ( const int exception[] )

   {
     cout << exception
          << "Array Out Of Bounds"
          << endl ;

     exit( 0 ) ;
   }
   
   for ( int i = 0 ; *word_ptr != NULL_CHAR ; word_ptr++, i++ )
   {
      if ( *word_ptr == SEARCH_CHAR )
      {
         indexOf_char = i ;
      }
   }
   
   if ( indexOf_char == INVALID_INDEX )
   {
      cout << "There is no '"
	   << SEARCH_CHAR
	   << "' in " 
	   << word 
	   << endl ;
   }
   else
   {
      cout << "Last '"
	   << SEARCH_CHAR
	   << "' in " 
	   << word 
	   << " has index " 
	   << indexOf_char 
	   << endl ;
   }
 
}


Output At Runtime:
Input a word: aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa
terminate called after throwing an instance of 'char const*'
Aborted

Thanks for any help!
Patfish is offline   Reply With Quote
Old Oct 29th, 2010, 11:07 AM   #2
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 19 DaWei will become famous soon enoughDaWei will become famous soon enough
Re: C++ Exception Handling

You've only allowed 20 characters for the word. What do you think happens if you exceed that length? Bad things, some more visibly damaging than others.

Since this is C++, why don't you use the string class and avoid the problem sensibly?
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 29th, 2010, 11:16 AM   #3
Patfish
Newbie
 
Patfish's Avatar
 
Join Date: Oct 2010
Posts: 8
Rep Power: 0 Patfish is on a distinguished road
Re: C++ Exception Handling

Hi Dawei,

The brief i have is to alter the original program (which is the code above minus the try throw catch) so that it terminates with the error message "Array Out Of Bounds" if a word longer than 20 characters is ended. As you can tell I'm pretty new to C++ and although I've covered some of classes its not within the scope of this assignment.
A throw function with this termination statement seemed the best (in my limited knowledge) way of doing this and I have managed to make it terminate but I cant get it to display the error message
Patfish is offline   Reply With Quote
Old Oct 29th, 2010, 11:23 AM   #4
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 19 DaWei will become famous soon enoughDaWei will become famous soon enough
Re: C++ Exception Handling

The problem is that you're invoking undefined behavior BEFORE you reach your throw. In this case you're clobbering other things on the stack.

In a typical implementation that would begin with SEARCH_CHAR and continue on, depending upon the length entered by the user. If you continue beyond your own variables and clobber the stack frame used by "main" your system is going to run off into the weeds and puke on its shoes.

If you MUST use a C-style string, then use an input function that allows you to restrict the input length, and declare enough chars to hold input of that length. See, for instance, "getline."
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Oct 29th, 2010, 11:27 AM   #5
Patfish
Newbie
 
Patfish's Avatar
 
Join Date: Oct 2010
Posts: 8
Rep Power: 0 Patfish is on a distinguished road
Re: C++ Exception Handling

thanks Dawei, i'll look into getline
Patfish is offline   Reply With Quote
Old Oct 29th, 2010, 11:50 AM   #6
L7Sqr
Professional Programmer
 
Join Date: Jun 2005
Location: here
Posts: 440
Rep Power: 10 L7Sqr will become famous soon enough
Re: C++ Exception Handling

You should be catching what you throw. Consider the following:
cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int main () {
  4. try {
  5. throw "Some error message";
  6. } catch ( const int msg[]) {
  7. std::cerr << "Got: " << msg << std::endl;
  8. return 0;
  9. }
  10. std::cerr << "No exception caught" << std::endl;
  11. return 0;
  12. }
Yields
terminate called after throwing an instance of 'char const*'
Aborted

cpp Syntax (Toggle Plain Text)
  1. #include <iostream>
  2.  
  3. int main () {
  4. try {
  5. throw "Some error message";
  6. } catch ( const char * msg) {
  7. std::cerr << "Got: " << msg << std::endl;
  8. return 0;
  9. }
  10. std::cerr << "No exception caught" << std::endl;
  11. return 0;
  12. }
Yields
Got: Some error message
Notice the difference.
__________________
"...and though our kids are blessed their parents let them shoulder all the blame."
- The Quiet Things That No One Ever Knows [BrandNew]

My Blog
L7Sqr is offline   Reply With Quote
Old Nov 2nd, 2010, 10:28 AM   #7
Patfish
Newbie
 
Patfish's Avatar
 
Join Date: Oct 2010
Posts: 8
Rep Power: 0 Patfish is on a distinguished road
Re: C++ Exception Handling

Thanks for the help on the above query guys, I've got that working nicely now but I also have a related problem on a similar subject.
I've another program which takes 5 in-putted numbers and displays them in reverse order. In its original state it has a cassert statement to terminate the program if anything but a digit is in-putted. My job is to get it to repeatedly ask for a digit until one is entered rather than just terminating. I have tried using another try throw catch with a for loop inside the catch statement but I cant seem to get it to work. I'd appreciate any comments/abuse on what I've got so far!

#include <iostream>

using namespace std;

int main( )
{
    const int NUMB_DIGITS = 5 ;

    int digits[ NUMB_DIGITS ]; 
   
   cout << "Enter " 
	<< NUMB_DIGITS
        << " digits, each on a new line:" 
	<< endl ;

   for ( int i = 0; i < NUMB_DIGITS ; i++ )

   {
     try
       {
         cin >> digits[ i ] ;

         if( ( digits[ i ] >= 0 ) && ( digits[ i ] <= 9 ) ) ;
	 { 
	   throw "Thats Not A Digit";
	 }
        
       }
     
     catch ( const char exception[] )

       {
	 for ( int i = 0; i < NUMB_DIGITS ; i++ );
	 {
	  cin >> digits[ i ];
	 }
       }

   }

   cout << "Digits in reverse order:" 
	<< endl ;

   for ( int j = NUMB_DIGITS - 1 ; j >= 0 ; j-- )
   {
     cout << digits[ j ] 
	  << endl ;
   }
}
Patfish is offline   Reply With Quote
Old Nov 2nd, 2010, 11:38 AM   #8
DaWei
Resident Grouch
 
DaWei's Avatar
 
Join Date: Jun 2005
Posts: 8,368
Rep Power: 19 DaWei will become famous soon enoughDaWei will become famous soon enough
Re: C++ Exception Handling

You're laboring under a misperception. If you ask the extraction operator (>>) for an integer and you give it something else it will not store that something else in some location for you to magically test for type. It will, rather, break the stream. The stream will then not function again until you have taken it down to the local repair shop and had it fixed.

Newbies don't know this st*ff, of course. They would actually have to read and understand the documentation for the functions they use.

See the methods, "good()," "bad()," "fail()," et. al.
__________________
Contributor's Corner:
Politically Incorrect
DaWei on Pointers
DaWei is offline   Reply With Quote
Old Nov 2nd, 2010, 6:16 PM   #9
Ancient Dragon
Achieved Level 70

 
Ancient Dragon's Avatar
 
Join Date: Jun 2005
Location: near St Louis, MO. (USA)
Posts: 4,345
Rep Power: 10 Ancient Dragon will become famous soon enoughAncient Dragon will become famous soon enough
Re: C++ Exception Handling

>>They would actually have to read and understand the documentation for the functions they use.

And that's one of the problems with today's modern programming -- no printed documentation. When you and I started program we had no choice but to buy programming books and read the printed documentation that came with our compilers -- Microsoft provided a shelf full of small to large programming technical books with their compilers, which is probably one reason their compilers were always to expensive. We did not have an internet on which we could post our problems -- we had to do all the research and tedious analysis ourselves. We quickly learned how to read documentation and knew where to find answers to questions. Today's new programmers normally don't know diddly shit about those things. Instant gratification is all they want.
__________________
PFO's FAQ is here
Forum Rules

There is no cow level (Diablo III)
If you never push yourself you will never improve (Diablo III)
Ancient Dragon is offline   Reply With Quote
Old Nov 2nd, 2010, 6:25 PM   #10
Patfish
Newbie
 
Patfish's Avatar
 
Join Date: Oct 2010
Posts: 8
Rep Power: 0 Patfish is on a distinguished road
Re: C++ Exception Handling

/me hides
Patfish is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

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 On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Painting simple ellips in C++ Builder. Exception raising. hazamin C++ 5 Mar 13th, 2009 1:15 PM
Query regarding extending Exception classes arj Java 1 Apr 25th, 2008 4:31 PM
exception handling l2u C++ 2 Mar 3rd, 2007 5:47 PM
coldfire assembly - rts causes Illegal Instruction Exception mika Assembly 4 Jun 18th, 2006 3:35 PM
AhhH!!! I can't find anything on this exception... stakeknife ASP 2 Sep 26th, 2005 7:48 AM




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 7:34 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC