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 [modified]

exceptions [modified]

Scheduled Pinned Locked Moved C / C++ / MFC
questionc++com
12 Posts 6 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.
  • B Offline
    B Offline
    big_denny_200
    wrote on last edited by
    #1

    hi again :) I saw in some code such thing : (it was a mathematical formula parser)

    try
    {
    ...
    }
    catch(CException1 pError)
    {
    ...
    }
    catch(CException2 pError)
    {
    ...
    }
    catch(CException3 pError)
    {
    ...
    }catch(CException4 pError)
    {
    ...
    }
    catch(CException5 pError)
    {
    ...
    }
    catch(CException6 pError)
    {
    ...
    }

    I have question : Is not it uncomfortable to write so many catches ? when all theses exceptions are some what related, could not we unite them under some CMainException class and catch only one exception ? this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp From the Using the Code section But the article is verry coool :rose: -- modified at 4:15 Monday 29th May, 2006

    C T S T 4 Replies Last reply
    0
    • B big_denny_200

      hi again :) I saw in some code such thing : (it was a mathematical formula parser)

      try
      {
      ...
      }
      catch(CException1 pError)
      {
      ...
      }
      catch(CException2 pError)
      {
      ...
      }
      catch(CException3 pError)
      {
      ...
      }catch(CException4 pError)
      {
      ...
      }
      catch(CException5 pError)
      {
      ...
      }
      catch(CException6 pError)
      {
      ...
      }

      I have question : Is not it uncomfortable to write so many catches ? when all theses exceptions are some what related, could not we unite them under some CMainException class and catch only one exception ? this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp From the Using the Code section But the article is verry coool :rose: -- modified at 4:15 Monday 29th May, 2006

      C Offline
      C Offline
      color Aljechin
      wrote on last edited by
      #2

      Catch(...) and then GetLastError() :rose:

      S T S T 4 Replies Last reply
      0
      • C color Aljechin

        Catch(...) and then GetLastError() :rose:

        S Offline
        S Offline
        Sarath C
        wrote on last edited by
        #3

        GetLastError will not help u in all cases. it applicable if only if the library set the relevant errors u may have to design ur own class if the exception is not provded by windows or the library u are using. if it is a generic one, then the code will be very less. there are centralized and decentralized error handling strategy. select one which suits for u. if u r trying to reduce code, go for centralized one. but the common routine or class will be bulky because of this :( SaRath

        1 Reply Last reply
        0
        • B big_denny_200

          hi again :) I saw in some code such thing : (it was a mathematical formula parser)

          try
          {
          ...
          }
          catch(CException1 pError)
          {
          ...
          }
          catch(CException2 pError)
          {
          ...
          }
          catch(CException3 pError)
          {
          ...
          }catch(CException4 pError)
          {
          ...
          }
          catch(CException5 pError)
          {
          ...
          }
          catch(CException6 pError)
          {
          ...
          }

          I have question : Is not it uncomfortable to write so many catches ? when all theses exceptions are some what related, could not we unite them under some CMainException class and catch only one exception ? this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp From the Using the Code section But the article is verry coool :rose: -- modified at 4:15 Monday 29th May, 2006

          T Offline
          T Offline
          ThatsAlok
          wrote on last edited by
          #4

          big_denny_200 wrote:

          this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp

          It would be better if you ask the author of article directly, by posting your query at bottom of article only! anyways you can make a Base class CMainException  and derived rest of class (i.e. CException1 ...) from them! and you can catch the any Exception in Base class Object

          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

          cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

          T 1 Reply Last reply
          0
          • C color Aljechin

            Catch(...) and then GetLastError() :rose:

            T Offline
            T Offline
            ThatsAlok
            wrote on last edited by
            #5

            Aljechin wrote:

            GetLastError()

            you can't get any error code or string using GetLastError if the Exception throwed is Custom!, GetLastError is Window specific api!

            "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

            cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

            1 Reply Last reply
            0
            • C color Aljechin

              Catch(...) and then GetLastError() :rose:

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

              In general catch(...) is bad form. There are exceptions such as when you do a catch all, perform some clean up and then re-throw, for example. A general rule of thumb with exception handling is that you should only catch what you expect to be thrown; a catch all violates this principle. Steve

              1 Reply Last reply
              0
              • B big_denny_200

                hi again :) I saw in some code such thing : (it was a mathematical formula parser)

                try
                {
                ...
                }
                catch(CException1 pError)
                {
                ...
                }
                catch(CException2 pError)
                {
                ...
                }
                catch(CException3 pError)
                {
                ...
                }catch(CException4 pError)
                {
                ...
                }
                catch(CException5 pError)
                {
                ...
                }
                catch(CException6 pError)
                {
                ...
                }

                I have question : Is not it uncomfortable to write so many catches ? when all theses exceptions are some what related, could not we unite them under some CMainException class and catch only one exception ? this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp From the Using the Code section But the article is verry coool :rose: -- modified at 4:15 Monday 29th May, 2006

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

                It is better to catch exceptions by reference rather then by value as is in your sample code. i.e. try { .. } catch(const CException1 & Error) { ... } // More stuff goes here. First it's more efficient as no copying occurs. More importantly it avoids slicing and thus allows you to make a catch class with virtual functions. This is one way to avoid catching so many different exceptions types: instead you catch a base class with virtual functions by reference and make use of polymorphism. Steve

                T 1 Reply Last reply
                0
                • B big_denny_200

                  hi again :) I saw in some code such thing : (it was a mathematical formula parser)

                  try
                  {
                  ...
                  }
                  catch(CException1 pError)
                  {
                  ...
                  }
                  catch(CException2 pError)
                  {
                  ...
                  }
                  catch(CException3 pError)
                  {
                  ...
                  }catch(CException4 pError)
                  {
                  ...
                  }
                  catch(CException5 pError)
                  {
                  ...
                  }
                  catch(CException6 pError)
                  {
                  ...
                  }

                  I have question : Is not it uncomfortable to write so many catches ? when all theses exceptions are some what related, could not we unite them under some CMainException class and catch only one exception ? this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp From the Using the Code section But the article is verry coool :rose: -- modified at 4:15 Monday 29th May, 2006

                  T Offline
                  T Offline
                  toxcct
                  wrote on last edited by
                  #8

                  hi denny, why didn't you ask this at the bottom of the VisualCalc[^] article ? i would have asked immediately... for your question, note that i don't catch the exceptions like you do (by copy : catch(CException1 pError)) but by reference (catch(CException1& pError)) which is trully different for the memory management. about the many catch blocs, there's no problem with that. you can have as many catch as you like without altering the performances... you can however reduce the number of catches (design matter) as i provide a base class for all the Parser exceptions (CVCAlcParserException class), but by doing this, you wouldn't be able to tell the user if he gets a syntax, a mathematic, or whatever kind of error... that's all i think. if you have any other questions, don't hesitate to ask on the article's message board ;)


                  TOXCCT >>> GEII power

                  [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                  1 Reply Last reply
                  0
                  • C color Aljechin

                    Catch(...) and then GetLastError() :rose:

                    T Offline
                    T Offline
                    toxcct
                    wrote on last edited by
                    #9

                    VisualCalc provides its own exception classes, so you will never get any accurate error message. moreover, be careful of the case sensitivity of the language. **C**atch is not a valid keyword


                    TOXCCT >>> GEII power

                    [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                    1 Reply Last reply
                    0
                    • T ThatsAlok

                      big_denny_200 wrote:

                      this code is from this article : http://www.codeproject.com/cpp/VisualCalc.asp

                      It would be better if you ask the author of article directly, by posting your query at bottom of article only! anyways you can make a Base class CMainException  and derived rest of class (i.e. CException1 ...) from them! and you can catch the any Exception in Base class Object

                      "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                      cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

                      T Offline
                      T Offline
                      toxcct
                      wrote on last edited by
                      #10

                      ThatsAlok wrote:

                      you can make a Base class CMainException and derived rest of class from them

                      like CVCalcParserException base class for example ? ;)


                      TOXCCT >>> GEII power

                      [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                      T 1 Reply Last reply
                      0
                      • S Stephen Hewitt

                        It is better to catch exceptions by reference rather then by value as is in your sample code. i.e. try { .. } catch(const CException1 & Error) { ... } // More stuff goes here. First it's more efficient as no copying occurs. More importantly it avoids slicing and thus allows you to make a catch class with virtual functions. This is one way to avoid catching so many different exceptions types: instead you catch a base class with virtual functions by reference and make use of polymorphism. Steve

                        T Offline
                        T Offline
                        toxcct
                        wrote on last edited by
                        #11

                        Stephen Hewitt wrote:

                        It is better to catch exceptions by reference rather then by value as is in your sample code

                        VisualCalc actually catches its exceptions by reference. the OP typed it wrong.

                        Stephen Hewitt wrote:

                        it [...] allows you to make a catch class with virtual functions

                        exactly what it does...

                        Stephen Hewitt wrote:

                        This is one way to avoid catching so many different exceptions types

                        it is also. actually, VisualCalc Parser provides a base class for all its exception classes, but i still catch each exception category one by one to provide a visual feedback to the calculator user... like this, he finally know if he did a syntax, mathematic of whatever kind of error in his expression...


                        TOXCCT >>> GEII power

                        [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                        1 Reply Last reply
                        0
                        • T toxcct

                          ThatsAlok wrote:

                          you can make a Base class CMainException and derived rest of class from them

                          like CVCalcParserException base class for example ? ;)


                          TOXCCT >>> GEII power

                          [VisualCalc 3.0  updated ][Flags Beginner's Guide  new! ]

                          T Offline
                          T Offline
                          ThatsAlok
                          wrote on last edited by
                          #12

                          toxcct wrote:

                          ike CVCalcParserException base class for example ? ;)

                          Thats why i asked him to post your problem in respective article Forum!,

                          "Opinions are neither right nor wrong. I cannot change your opinion. I can, however, change what influences your opinion." - David Crow

                          cheers, Alok Gupta VC Forum Q&A :- I/ IV Support CRY- Child Relief and You

                          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