Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Anything like C/C++ _LINE_ in JAVA ?

Posted on 1998-08-28
4
Medium Priority
?
298 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 400 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

Enroll in October's Free Course of the Month

Do you work with and analyze data? Enroll in October's Course of the Month for 7+ hours of SQL training, allowing you to quickly and efficiently store or retrieve data. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
Suggested Courses

636 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