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 exception

try-catch exception

Scheduled Pinned Locked Moved C / C++ / MFC
help
6 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.
  • P Offline
    P Offline
    Pryabu
    wrote on last edited by
    #1

    Hi, my application is a win32 appln. I have added try-catch loop. But its showing the following error.Please help me to resolve this error. Code: try { fn(); } catch(CException exp) { exp->GetErrorMessage(lpszErr,sizeof(lpszErr),NULL); wsprintf(lpszLogMsg,"%s\\%s",lpszErr,"fn"); WriteLog(0,lpszLogMsg); return; } Error: error C2061: syntax error : identifier 'CException' error C2310: catch handlers must specify one type error C2227: left of '->GetErrorMessage' must point to class/struct/union error C2317: 'try' block starting on line '474' has no catch handlers

    S N 2 Replies Last reply
    0
    • P Pryabu

      Hi, my application is a win32 appln. I have added try-catch loop. But its showing the following error.Please help me to resolve this error. Code: try { fn(); } catch(CException exp) { exp->GetErrorMessage(lpszErr,sizeof(lpszErr),NULL); wsprintf(lpszLogMsg,"%s\\%s",lpszErr,"fn"); WriteLog(0,lpszLogMsg); return; } Error: error C2061: syntax error : identifier 'CException' error C2310: catch handlers must specify one type error C2227: left of '->GetErrorMessage' must point to class/struct/union error C2317: 'try' block starting on line '474' has no catch handlers

      S Offline
      S Offline
      ShilpiP
      wrote on last edited by
      #2

      try
      {
      fn();
      }
      catch(Exception *exp)
      {
      exp->GetErrorMessage(lpszErr,sizeof(lpszErr),NULL);
      wsprintf(lpszLogMsg,"%s\\%s",lpszErr,"fn");
      WriteLog(0,lpszLogMsg);
      return;
      }

      Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

      P 1 Reply Last reply
      0
      • P Pryabu

        Hi, my application is a win32 appln. I have added try-catch loop. But its showing the following error.Please help me to resolve this error. Code: try { fn(); } catch(CException exp) { exp->GetErrorMessage(lpszErr,sizeof(lpszErr),NULL); wsprintf(lpszLogMsg,"%s\\%s",lpszErr,"fn"); WriteLog(0,lpszLogMsg); return; } Error: error C2061: syntax error : identifier 'CException' error C2310: catch handlers must specify one type error C2227: left of '->GetErrorMessage' must point to class/struct/union error C2317: 'try' block starting on line '474' has no catch handlers

        N Offline
        N Offline
        Niklas L
        wrote on last edited by
        #3

        The exception type you are trying to catch has to be defined. The error simply reads: I don't know what CException is. You might be missing an #include, defining CException. There is no predefined exception class in C++ as in many other languages. In C++ you can throw whatever object you want; int, const char*, CMyComplexClass... Always try to catch exceptions by reference instead of by value. That will avoid a lot of unnecessary copying of data. catch(CException &exp)

        home

        A 1 Reply Last reply
        0
        • S ShilpiP

          try
          {
          fn();
          }
          catch(Exception *exp)
          {
          exp->GetErrorMessage(lpszErr,sizeof(lpszErr),NULL);
          wsprintf(lpszLogMsg,"%s\\%s",lpszErr,"fn");
          WriteLog(0,lpszLogMsg);
          return;
          }

          Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

          P Offline
          P Offline
          Pryabu
          wrote on last edited by
          #4

          now the errors has gone.Can you please tell me how to get the error message. ni CExeception we are having GetErrorMessage() fn. Similarly, what is the fn in exception class to get the error message?

          S 1 Reply Last reply
          0
          • P Pryabu

            now the errors has gone.Can you please tell me how to get the error message. ni CExeception we are having GetErrorMessage() fn. Similarly, what is the fn in exception class to get the error message?

            S Offline
            S Offline
            ShilpiP
            wrote on last edited by
            #5

            Print this line to get the error message

            exp->Message

            Yes U Can ...If U Can ,Dream it , U can do it ...ICAN

            1 Reply Last reply
            0
            • N Niklas L

              The exception type you are trying to catch has to be defined. The error simply reads: I don't know what CException is. You might be missing an #include, defining CException. There is no predefined exception class in C++ as in many other languages. In C++ you can throw whatever object you want; int, const char*, CMyComplexClass... Always try to catch exceptions by reference instead of by value. That will avoid a lot of unnecessary copying of data. catch(CException &exp)

              home

              A Offline
              A Offline
              Aescleal
              wrote on last edited by
              #6

              To amplify Niklas' answer a bit... Throw by value, catch by reference. If you do this: - It removes copying of the exception, reducing the chance of further exceptions being thrown - You can catch families of exceptions polymorphically Cheers, Ash

              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