Thread: try catch throw in c++

  1. #1
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    try catch throw in c++

    after i try the 1st 1 from this web...
    Cprogramming.com FAQ > Throwing exceptions (C++)
    it says end1 is nnot a member of 'std'
    how to solve this problem
    oso i still dun understand how to use this for my project D:
    pls help me

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You should be using endl, not end1 (as in lowercase L, not one).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    thx for helping -just joined....

  4. #4
    Registered User
    Join Date
    Dec 2010
    Posts
    5
    is there a simple and easy example to use3 try catch throw....

  5. #5
    Registered User
    Join Date
    Dec 2010
    Posts
    5

    after testing and trying i found some problems....

    ZZZZ
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <conio.h>
    #include <iostream>
    int func(void);
    void func2(void);
    
    int func(void)
    {
        int total1,seconds,minutes;
       do{
            try
            {
                system ("cls");
                printf("\nTask 4: Time count\n");
                printf("==================\n");
                printf("Please enter how many minutes to count down <5 minutes or less>:");
                scanf("%d",&minutes);
                if(minutes>=0&&minutes<6)
                  {
                        printf("\n%02d:00\n",minutes);
                        _sleep(100);
                        minutes--;
                        for(;minutes>=0;minutes--) 
                        {  
                           for(seconds=59;seconds>=0;seconds--)
                          {  
                           printf("%02d:%02d\n",minutes,seconds);
                           _sleep(100);
                          }
                         }
                         _sleep(100);
                  }
                  else
               {
                system ("cls");
                throw "out of range";
                } 
                
                }
            
            catch (const char *e)
            {
             
             std ::cerr <<e<<std::endl;;   
            } 
            _sleep(100);
        }while(minutes>=6||-1>minutes); 
    }
    
    int main()
    {
        func();
        return (0);
        
    }

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    It is silly to throw and catch an exception here. You should just perform that range check and act accordingly.

    Furthermore, you should make your indentation more consistent. Unless you have special reasons to do otherwise, you should also use C++ style I/O rather than C style I/O. If you really did need to throw an exception, #include <stdexcept> and thow std::out_of_range rather than a string literal.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Throw catch question
    By TriKri in forum C++ Programming
    Replies: 2
    Last Post: 10-25-2009, 03:32 AM
  2. Exceptions "try, catch, and throw Statements" ???
    By Loic in forum C++ Programming
    Replies: 2
    Last Post: 08-12-2008, 09:22 PM
  3. Throw Catch for External Program
    By dav_mt in forum C++ Programming
    Replies: 6
    Last Post: 04-19-2008, 09:52 AM
  4. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  5. Try...catch...throw or ifs?
    By Kylecito in forum C++ Programming
    Replies: 9
    Last Post: 03-02-2006, 10:41 PM