Solved

Anything like C/C++ _LINE_ in JAVA ?

Posted on 1998-08-28
4
256 Views
Last Modified: 2008-02-01
Does anyone have any code that will do what the _LINE_ in C/C++ does when logging messages?

Thanks in advance !
0
Comment
Question by:jefftk
  • 2
4 Comments
 

Author Comment

by:jefftk
ID: 1222806
Adjusted points to 100
0
 
LVL 5

Expert Comment

by:msmolyak
ID: 1222807
The line number is contained in a stack trace which gets generated when the exception is thrown (if the code is compiled in debug mode). Can you simply print a stack trace if something happens (otherwise why do you need a line number)?
0
 

Author Comment

by:jefftk
ID: 1222808
I am trying to simulate the C/C++ environment...
There are _FILE_ and _LINE_  macros the supply the program name and line number to the logging facility...
0
 
LVL 7

Accepted Solution

by:
yoren earned 100 total points
ID: 1222809
There isn't a direct macro to give you the file and line numbers. If you're into kludges, however, you can do the following:

1. throw an exception
2. catch it
3. capture the stack trace in a string
4. parse it to get the file and line number

Here are steps 1-3:

    try {
      throw new Exception();
    }
    catch(Exception e) {
        StringBuffer sbTrace;
        StringWriter sw = new StringWriter();
        e.printStackTrace(new PrintWriter(sw));
       
        sbTrace = sw.getBuffer();

        // sbTrace has the error message that includes
        // the file and line number
    }


0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java Flight Recorder and Java Mission Control together create a complete tool chain to continuously collect low level and detailed runtime information enabling after-the-fact incident analysis. Java Flight Recorder is a profiling and event collectio…
This video teaches viewers about errors in exception handling.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

932 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

8 Experts available now in Live!

Get 1:1 Help Now