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. Exceptions, Throwing, Handling

Exceptions, Throwing, Handling

Scheduled Pinned Locked Moved C / C++ / MFC
questionhardware
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.
  • S Offline
    S Offline
    ScotDolan
    wrote on last edited by
    #1

    I have application that uses a serial port and I/O card. The application starts up add attempts to initializes the serial card and I/O card. If either of them fail and would like to throw an exception telling the user that corresponding object failed and terminate the applications. How do i accomplish this? I have tried to use throw (" Unable to initialize Serial Port" ); However, it does not display my string. It does terminate the application but, the user is left clue unless..

    Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

    E J I 4 Replies Last reply
    0
    • S ScotDolan

      I have application that uses a serial port and I/O card. The application starts up add attempts to initializes the serial card and I/O card. If either of them fail and would like to throw an exception telling the user that corresponding object failed and terminate the applications. How do i accomplish this? I have tried to use throw (" Unable to initialize Serial Port" ); However, it does not display my string. It does terminate the application but, the user is left clue unless..

      Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

      E Offline
      E Offline
      Eytukan
      wrote on last edited by
      #2

      If you want to implement something quick & throw just strings you can use std::exception

      void main()
      {
      try
      {
      std::exception ex("My error");
      throw ex;

      }
      catch(std::exception& ex)
      {
      cout<< ex.what();
      }

      }

      The best way is to design your own exception class to deal with different cases -or even a struct, As C++ is quite flexible when it comes to throwing something ;)

      He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

      1 Reply Last reply
      0
      • S ScotDolan

        I have application that uses a serial port and I/O card. The application starts up add attempts to initializes the serial card and I/O card. If either of them fail and would like to throw an exception telling the user that corresponding object failed and terminate the applications. How do i accomplish this? I have tried to use throw (" Unable to initialize Serial Port" ); However, it does not display my string. It does terminate the application but, the user is left clue unless..

        Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

        E Offline
        E Offline
        Eytukan
        wrote on last edited by
        #3

        Oops sorry. I was too quick to respond. If you want to catch any exception and then throw a meaningful one , you should do like

        void IOReadFuntion()
        {
        try
        {
        // IO functions.
        }
        catch(...)
        {
        std::exception ex("Error while reading port");
        throw ex;
        }
        }

        void main()
        {
        try
        {
        IOReadFuntion();
        IOWriteFunction();

        }
        catch(std::exception& ex)
        {
        cout<<ex.what()
        }

        }

        Signing off. good night! :zzz:

        He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

        S 1 Reply Last reply
        0
        • E Eytukan

          Oops sorry. I was too quick to respond. If you want to catch any exception and then throw a meaningful one , you should do like

          void IOReadFuntion()
          {
          try
          {
          // IO functions.
          }
          catch(...)
          {
          std::exception ex("Error while reading port");
          throw ex;
          }
          }

          void main()
          {
          try
          {
          IOReadFuntion();
          IOWriteFunction();

          }
          catch(std::exception& ex)
          {
          cout<<ex.what()
          }

          }

          Signing off. good night! :zzz:

          He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

          S Offline
          S Offline
          ScotDolan
          wrote on last edited by
          #4

          Vunic, I am not using a console application. I have MFC application and when i throw and exception it creates window that say Microsft Visual C++ Runtime Error. It has big red x and Has the path of the exe that failed, along with a string telling the failure. I wanna use my string instead of the generic string provided.

          Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

          E 1 Reply Last reply
          0
          • S ScotDolan

            I have application that uses a serial port and I/O card. The application starts up add attempts to initializes the serial card and I/O card. If either of them fail and would like to throw an exception telling the user that corresponding object failed and terminate the applications. How do i accomplish this? I have tried to use throw (" Unable to initialize Serial Port" ); However, it does not display my string. It does terminate the application but, the user is left clue unless..

            Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

            J Offline
            J Offline
            Joe Woodbury
            wrote on last edited by
            #5

            Why use an exception? Why not just attempt to initialize the serial card and return an error code on failure? I actually have code that does exactly that (our system uses a lot of serial ports.) Most our code doesn't use MFC, but the few programs that do, I do this check in the InitInstance() function. On failure, it pops up a message box explaining the failure. (For some applications, I return a resource ID of the error string; in others just a bool indicating success or failure--it all depends on the requirements.)

            Anyone who thinks he has a better idea of what's good for people than people do is a swine. - P.J. O'Rourke

            1 Reply Last reply
            0
            • S ScotDolan

              Vunic, I am not using a console application. I have MFC application and when i throw and exception it creates window that say Microsft Visual C++ Runtime Error. It has big red x and Has the path of the exe that failed, along with a string telling the failure. I wanna use my string instead of the generic string provided.

              Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

              E Offline
              E Offline
              Eytukan
              wrote on last edited by
              #6

              ScotDolan wrote:

              when i throw and exception it creates window that say Microsft Visual C++ Runtime Error.

              No way, when you throw your own exception. And if you are handling it, it should work. Try this one. No matter it is console or dialog.

                  try
              {
              std::exception ex("MyExeception");
              throw ex;
              }
              catch(std::exception& ex)
              {
              	AfxMessageBox(ex.what());
              	
              }
              

              btw, if you want to catch any unhandled exception, you could try SetUnhandledExceptionFilter()

              He never answers anyone who replies to him. I've taken to calling him a retard, which is not fair to retards everywhere.-Christian Graus

              1 Reply Last reply
              0
              • S ScotDolan

                I have application that uses a serial port and I/O card. The application starts up add attempts to initializes the serial card and I/O card. If either of them fail and would like to throw an exception telling the user that corresponding object failed and terminate the applications. How do i accomplish this? I have tried to use throw (" Unable to initialize Serial Port" ); However, it does not display my string. It does terminate the application but, the user is left clue unless..

                Scott Dolan Jernie Corporation Engineering & Manufacturing Software, Hardware, & Enclosures

                I Offline
                I Offline
                Iain Clarke Warrior Programmer
                wrote on last edited by
                #7

                I'd second Joe's reply - why on earth are you using exceptions for this. A serial port being unavailable is by its nature a normal thing. Not _exception_al at all! Why not:

                BOOL CMyDialog::InitialisePort (UINT nComPort, CString &Error)
                {
                ...
                if (!...)
                {
                Error = _T(" Unable to initialize Serial Port" );
                return FALSE;
                }
                ...
                return TRUE;
                }

                ? I can't think of a single place I've written a throw command. I've written a few catchers for other people's libraries, so I'm not a exception virgin - just not a fan. Good luck with your application, Iain.

                Codeproject MVP for C++, I can't believe it's for my lounge posts...

                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