Solved

Anything like C/C++ _LINE_ in JAVA ?

Posted on 1998-08-28
4
245 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
Comment Utility
Adjusted points to 100
0
 
LVL 5

Expert Comment

by:msmolyak
Comment Utility
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
Comment Utility
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
Comment Utility
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bunnyEars challenge 6 62
count11 challenge 6 47
XML Paring  Error - Premature end of file. 7 55
topping1 challenge 7 47
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

762 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

11 Experts available now in Live!

Get 1:1 Help Now