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 - getting line number

try/catch - getting line number

Scheduled Pinned Locked Moved C / C++ / MFC
question
5 Posts 3 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.
  • C Offline
    C Offline
    Chintoo723
    wrote on last edited by
    #1

    I have a try/catch block that is throwing an exception. What are the different things I can find out about the exception? line number, exception type, etc? thanks!

    _ S 2 Replies Last reply
    0
    • C Chintoo723

      I have a try/catch block that is throwing an exception. What are the different things I can find out about the exception? line number, exception type, etc? thanks!

      _ Offline
      _ Offline
      __yb
      wrote on last edited by
      #2

      simply put - what u throw is what u get. try/catch are not defined to give info like line numbers, only what was thrown. to get more info u'll have to use SEH, but then u lose unwind semantics...

      1 Reply Last reply
      0
      • C Chintoo723

        I have a try/catch block that is throwing an exception. What are the different things I can find out about the exception? line number, exception type, etc? thanks!

        S Offline
        S Offline
        Shraddhan
        wrote on last edited by
        #3

        What I do is to buld a string containing the value of __LINE__ and pass this to a global ThrowError function which packages this into an object which throw can throw. For example: class CMyError : public CException { public: const char * Message; }; and somewhere in the code: static CString s; s.Format("File %s, line %d\nOverflow in addition", THIS_FILE, __LINE__); ThrowError(s); where ThrowError puts its argument into Message in an instance of CMyError and throws the CMyError. So basically I can send any information I want to via a throw. Shraddhan

        C 1 Reply Last reply
        0
        • S Shraddhan

          What I do is to buld a string containing the value of __LINE__ and pass this to a global ThrowError function which packages this into an object which throw can throw. For example: class CMyError : public CException { public: const char * Message; }; and somewhere in the code: static CString s; s.Format("File %s, line %d\nOverflow in addition", THIS_FILE, __LINE__); ThrowError(s); where ThrowError puts its argument into Message in an instance of CMyError and throws the CMyError. So basically I can send any information I want to via a throw. Shraddhan

          C Offline
          C Offline
          Chintoo723
          wrote on last edited by
          #4

          1. Thanks for the suggestion, but I think that is not really what I want. If I know where to throw the error I know where the error is really happening. What I was looking for is line number for unexpected errors. 2. To __yb: I learnt that you can do all with C++ try/catch that you do with SEH through _set_se_translator. So if you can use SEH to get the instruction pointer that caused the error, you can obtain the same using C++ try/catch too. 3. For a suggestion to my own query, I can think of a _dirty_ way to obtain the line number in a try/catch or even with SEH. Before compiling the code write a script that will insert this statement after every line of code: g_lineno = __LINE__; and then in your exception filter, you can print this global variable g_lineno. Now what I want to know from you guys is, whether this is going to be a performance hit if I implement it in production. I think _no_big_deal_ but what is your opinion? thanks!

          S 1 Reply Last reply
          0
          • C Chintoo723

            1. Thanks for the suggestion, but I think that is not really what I want. If I know where to throw the error I know where the error is really happening. What I was looking for is line number for unexpected errors. 2. To __yb: I learnt that you can do all with C++ try/catch that you do with SEH through _set_se_translator. So if you can use SEH to get the instruction pointer that caused the error, you can obtain the same using C++ try/catch too. 3. For a suggestion to my own query, I can think of a _dirty_ way to obtain the line number in a try/catch or even with SEH. Before compiling the code write a script that will insert this statement after every line of code: g_lineno = __LINE__; and then in your exception filter, you can print this global variable g_lineno. Now what I want to know from you guys is, whether this is going to be a performance hit if I implement it in production. I think _no_big_deal_ but what is your opinion? thanks!

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

            Brundiez wrote:

            Now what I want to know from you guys is, whether this is going to be a performance hit if I implement it in production. I think _no_big_deal_ but what is your opinion?

            My understanding is that there are quite large overheads in doing a throw / catch. Also, what happens afterwards? Does the code keep running, or do you inform the user? Either way, I reckon it is no big deal. Especially when you include the time taken to actually deal with the line number information. The cost of storing the line number in a global variable is nothing in comparison. But I've just noticed that you say:

            Brundiez wrote:

            Before compiling the code write a script that will insert this statement after every line of code

            *Every* line of code? Do you *really* need to know so precisely? Shraddhan

            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