Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. try catch exceptions

try catch exceptions

Scheduled Pinned Locked Moved C / C++ / MFC
help
7 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • G Offline
    G Offline
    gmallax
    wrote on last edited by
    #1

    hi, for(int i = 0;i < n; i++) { try { //......... } catch(exception e) { //........ } } In tha above code,in catch block im printing some error message after the for loop terminated.my problem is when the for loop get terminates,im having the last error message only(the catch is called 2 times,so i need to display 2 error messages)... pls help me...

    C _ S 3 Replies Last reply
    0
    • G gmallax

      hi, for(int i = 0;i < n; i++) { try { //......... } catch(exception e) { //........ } } In tha above code,in catch block im printing some error message after the for loop terminated.my problem is when the for loop get terminates,im having the last error message only(the catch is called 2 times,so i need to display 2 error messages)... pls help me...

      C Offline
      C Offline
      Cedric Moonen
      wrote on last edited by
      #2

      Gomathy_84 wrote:

      im printing some error message after the for loop terminated

      What do you mean by printing exactly ? Print on the console with printf ? Furthermore, you don't print after the for loop is terminated but in the loop itself.

      Cédric Moonen Software developer
      Charting control [v3.0] OpenGL game tutorial in C++

      1 Reply Last reply
      0
      • G gmallax

        hi, for(int i = 0;i < n; i++) { try { //......... } catch(exception e) { //........ } } In tha above code,in catch block im printing some error message after the for loop terminated.my problem is when the for loop get terminates,im having the last error message only(the catch is called 2 times,so i need to display 2 error messages)... pls help me...

        _ Offline
        _ Offline
        _Superman_
        wrote on last edited by
        #3

        From what I understand, you're collecting error message inside the catch block and then displaying it after the loop terminates. If so you will need to collect multiple strings. You can do this using a vector of strings.

        std::vector vstrErrors;

        try
        {
        }
        catch(std::exception e)
        {
        vstrErrors.push_back(e.what());
        }

        «_Superman_» I love work. It gives me something to do between weekends.
        Microsoft MVP (Visual C++)

        G 2 Replies Last reply
        0
        • G gmallax

          hi, for(int i = 0;i < n; i++) { try { //......... } catch(exception e) { //........ } } In tha above code,in catch block im printing some error message after the for loop terminated.my problem is when the for loop get terminates,im having the last error message only(the catch is called 2 times,so i need to display 2 error messages)... pls help me...

          S Offline
          S Offline
          Stephen Hewitt
          wrote on last edited by
          #4

          In general it's best to catch exceptions by reference, generally a const reference.

          try
          {
          // Exceptional stuff goes here...
          }
          catch (const exception &e)
          {
          // Handle exception here...
          }

          Steve

          1 Reply Last reply
          0
          • _ _Superman_

            From what I understand, you're collecting error message inside the catch block and then displaying it after the loop terminates. If so you will need to collect multiple strings. You can do this using a vector of strings.

            std::vector vstrErrors;

            try
            {
            }
            catch(std::exception e)
            {
            vstrErrors.push_back(e.what());
            }

            «_Superman_» I love work. It gives me something to do between weekends.
            Microsoft MVP (Visual C++)

            G Offline
            G Offline
            gmallax
            wrote on last edited by
            #5

            Yes...ThankYou so much :)

            1 Reply Last reply
            0
            • _ _Superman_

              From what I understand, you're collecting error message inside the catch block and then displaying it after the loop terminates. If so you will need to collect multiple strings. You can do this using a vector of strings.

              std::vector vstrErrors;

              try
              {
              }
              catch(std::exception e)
              {
              vstrErrors.push_back(e.what());
              }

              «_Superman_» I love work. It gives me something to do between weekends.
              Microsoft MVP (Visual C++)

              G Offline
              G Offline
              gmallax
              wrote on last edited by
              #6

              here,im having seperate exception class like connectionException class.I derived it from std::exception. In the place of throw ..i wrote as throw connectionException(...some error message). In the place of Catch...how to catch my class exception and covert into std::exception??? Thanx.

              _ 1 Reply Last reply
              0
              • G gmallax

                here,im having seperate exception class like connectionException class.I derived it from std::exception. In the place of throw ..i wrote as throw connectionException(...some error message). In the place of Catch...how to catch my class exception and covert into std::exception??? Thanx.

                _ Offline
                _ Offline
                _Superman_
                wrote on last edited by
                #7

                This should do it. This is the C++ concept of "a base class object can reference a derived class object".

                catch (connectionException& ce)
                {
                std::exception& e = ce;
                }

                «_Superman_» I love work. It gives me something to do between weekends.
                Microsoft MVP (Visual C++)

                1 Reply Last reply
                0
                Reply
                • Reply as topic
                Log in to reply
                • Oldest to Newest
                • Newest to Oldest
                • Most Votes


                • Login

                • Don't have an account? Register

                • Login or register to search.
                • First post
                  Last post
                0
                • Categories
                • Recent
                • Tags
                • Popular
                • World
                • Users
                • Groups