Printing line numbers in exceptions like in Java?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Digital Puer

    Printing line numbers in exceptions like in Java?

    Hi, I am interested in catching exceptions and printing the
    line number and file name of where the exception occurred,
    like what is done in Java. For example, when vector's at()
    accesses beyond its size and throws an exception, I would like
    to have the file and line information available.

    Any ideas how I can do this?

  • mlimber

    #2
    Re: Printing line numbers in exceptions like in Java?

    On Mar 1, 1:18 pm, "Digital Puer" <digital_p...@h otmail.comwrote :
    Hi, I am interested in catching exceptions and printing the
    line number and file name of where the exception occurred,
    like what is done in Java. For example, when vector's at()
    accesses beyond its size and throws an exception, I would like
    to have the file and line information available.
    >
    Any ideas how I can do this?
    Use the __FILE__ and __LINE__ macros as part of your exception
    message. If you're throwing your own exception with a constructor like
    this:

    MyException(
    const char* msg,
    const char* filename,
    unsigned line );

    you can create a macro to assist... something like:

    #define THROW_MY_EXCEPT ION( msg ) throw MyException( (msg), __FILE__,
    __LINE__ )

    If you're wanting to get that info from the standard library (or any
    other piece of code over which you don't have control), you'll have to
    either use your debugger (many can stop a the point when an exception
    is thrown) or catch the exception and throw a new one:

    try
    {
    vector<intv;
    cout << v.at( 0 );
    }
    catch( const std::exception& e )
    {
    THROW_MY_EXCEPT ION( e.what() );
    }

    Cheers! --M

    Comment

    • kwikius

      #3
      Re: Printing line numbers in exceptions like in Java?

      On 1 Mar, 18:18, "Digital Puer" <digital_p...@h otmail.comwrote :
      Hi, I am interested in catching exceptions and printing the
      line number and file name of where the exception occurred,
      like what is done in Java. For example, when vector's at()
      accesses beyond its size and throws an exception, I would like
      to have the file and line information available.
      >
      Any ideas how I can do this?
      Below is one simple way to do it. Not recommended for critical errors
      though, but OK for debugging. You could also look at how the assert
      macro works.

      regards
      Andy Little

      #include <stdexcept>
      #include <iostream>
      #include <sstream>

      std::string line_info(std:: string const & error, char const * file ,
      long line )
      {
      std::stringstre am s;
      s << "EXCEPTION: " << error << " in \"" << file << "\",line:" <<
      line;
      return s.str();
      }
      int main()
      {
      try{
      throw std::domain_err or( line_info( "Something went
      wrong",__FILE__ ,__LINE__));
      }
      catch(std::exce ption & e){
      std::cout << e.what() <<'\n';
      }
      }

      Comment

      • John Harrison

        #4
        Re: Printing line numbers in exceptions like in Java?

        Digital Puer wrote:
        Hi, I am interested in catching exceptions and printing the
        line number and file name of where the exception occurred,
        like what is done in Java. For example, when vector's at()
        accesses beyond its size and throws an exception, I would like
        to have the file and line information available.
        >
        Any ideas how I can do this?
        >
        I recommend using your debugger. Find the line of code in vector::at
        that throws the exception, use your debugger to set a break point on
        that line, then run your program and wait for the break point to be hit.
        Then check your call stack to see where vector::at was called from.

        Most debuggers cope pretty well with the STL these days.

        john

        Comment

        • Ian Collins

          #5
          Re: Printing line numbers in exceptions like in Java?

          John Harrison wrote:
          Digital Puer wrote:
          >
          >Hi, I am interested in catching exceptions and printing the
          >line number and file name of where the exception occurred,
          >like what is done in Java. For example, when vector's at()
          >accesses beyond its size and throws an exception, I would like
          >to have the file and line information available.
          >>
          >Any ideas how I can do this?
          >>
          >
          I recommend using your debugger. Find the line of code in vector::at
          that throws the exception, use your debugger to set a break point on
          that line, then run your program and wait for the break point to be hit.
          Then check your call stack to see where vector::at was called from.
          >
          Probably better to set the breakpoint on the exception constructor, if
          your STL code is optimised finding where the exception is thrown won't
          be easy. Probably not easy if it isn't as well!

          --
          Ian Collins.

          Comment

          • mlimber

            #6
            Re: Printing line numbers in exceptions like in Java?

            On Mar 1, 4:10 pm, Ian Collins <ian-n...@hotmail.co mwrote:
            John Harrison wrote:
            Digital Puer wrote:
            >
            Hi, I am interested in catching exceptions and printing the
            line number and file name of where the exception occurred,
            like what is done in Java. For example, when vector's at()
            accesses beyond its size and throws an exception, I would like
            to have the file and line information available.
            >
            Any ideas how I can do this?
            >
            I recommend using your debugger. Find the line of code in vector::at
            that throws the exception, use your debugger to set a break point on
            that line, then run your program and wait for the break point to be hit.
            Then check your call stack to see where vector::at was called from.
            >
            Probably better to set the breakpoint on the exception constructor, if
            your STL code is optimised finding where the exception is thrown won't
            be easy. Probably not easy if it isn't as well!
            Possibly better still is setting the debugger to break automatically
            when *any* exception is thrown. Of course, there are environments
            where (good) debuggers are not available, in which case the methods I
            suggested previously may prove more helpful.

            Cheers! --M

            Comment

            • Digital Puer

              #7
              Re: Printing line numbers in exceptions like in Java?

              On Mar 1, 12:58 pm, John Harrison <john_androni.. .@hotmail.comwr ote:
              Digital Puer wrote:
              Hi, I am interested in catching exceptions and printing the
              line number and file name of where the exception occurred,
              like what is done in Java. For example, when vector's at()
              accesses beyond its size and throws an exception, I would like
              to have the file and line information available.
              >
              Any ideas how I can do this?
              >
              I recommend using your debugger. Find the line of code in vector::at
              that throws the exception, use your debugger to set a break point on
              that line, then run your program and wait for the break point to be hit.
              Then check your call stack to see where vector::at was called from.
              >
              Most debuggers cope pretty well with the STL these days.
              >
              john


              This code is going into in-house production, so it will not be running
              with a debugger. I am particularly interested in getting the line
              number
              and filename from exceptions thrown by the STL data structures.

              Comment

              • mlimber

                #8
                Re: Printing line numbers in exceptions like in Java?

                On Mar 1, 6:29 pm, "Digital Puer" <digital_p...@h otmail.comwrote :
                This code is going into in-house production, so it will not be running
                with a debugger. I am particularly interested in getting the line
                number
                and filename from exceptions thrown by the STL data structures.
                Then you have several options: modify your standard library (don't do
                it!), ask your library vendor to add such functionality, get a new
                library that already supports this, or catch, translate, and throw as
                I described elsethread.

                Cheers! --M

                Comment

                Working...