Solved

get method and class names

Posted on 1998-02-06
6
148 Views
Last Modified: 2010-04-16
I have created a class to format error messages.  I
would like to use it instead of things like
System.out.println().  I
would like this object to tell me who called it.
Is the a way to discover the class and method name of
the caller?  printStackTrace() would be nice but it looks
like I need to catch().
0
Comment
Question by:hank1
  • 4
  • 2
6 Comments
 
LVL 16

Expert Comment

by:imladris
Comment Utility
printStackTrace will work fine. In your error class this code:

Throwable e=new Throwable();
ByteArrayOutputStream bo;
e.printStackTrace(new PrintStream(bo=new                                                     ByteArrayOutputStream()));
String stktrc=bo.toString();

Now the String stktrc contains a stacktrace, and you can slice it and dice it any way you like; print the first line, or the second line, or the whole thing!

0
 
LVL 1

Author Comment

by:hank1
Comment Utility
Works!  But let me follow up 1st.  I get a deprecation
notice.  Is this something I can correct or should I close
my eyes?

+5S:\lab\688sys\>javac -deprecation CtlLogger.java
CtlLogger.java:21: Note: The constructor java.io.PrintStream(java.io.OutputSt
ream) has been deprecated.
    e.printStackTrace(new PrintStream(bo =
                      ^
Note: CtlLogger.java uses a deprecated API.  Please consult the documentation
 for a better alternative.
2 warnings
0
 
LVL 16

Accepted Solution

by:
imladris earned 50 total points
Comment Utility
I'm not sure whether there are better alternatives or not.
I am still on Java 1.0.2. I would check what documentation you
have (or the standard class source code itself) for other
available variations of printStackTrace. I have seen other notes
go by, however, that the upgrade was not always done
consistently. Some things still require objects to be passed to
them (like PrintStream) which have become deprecated.
Anyway, if 1.1 contains a printStackTrace variation that you
prefer, or that avoids the deprecation warning, then it should
work with essentially the same tactic.

0
Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

 
LVL 1

Author Comment

by:hank1
Comment Utility
Thanks alot.
0
 
LVL 1

Author Comment

by:hank1
Comment Utility
ps  Use PrintWriter instead of PrintStream in 1.1
0
 
LVL 1

Author Comment

by:hank1
Comment Utility
Spoke too soon.  It compiles with printwriter but
no stack info.
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
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…

771 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