Solved

get method and class names

Posted on 1998-02-06
6
150 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
ID: 1232925
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
ID: 1232926
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
ID: 1232927
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
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.

 
LVL 1

Author Comment

by:hank1
ID: 1232928
Thanks alot.
0
 
LVL 1

Author Comment

by:hank1
ID: 1232929
ps  Use PrintWriter instead of PrintStream in 1.1
0
 
LVL 1

Author Comment

by:hank1
ID: 1232930
Spoke too soon.  It compiles with printwriter but
no stack info.
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

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…
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…
Viewers learn about the “for” loop and how it works in Java. By comparing it to the while loop learned before, viewers can make the transition easily. You will learn about the formatting of the for loop as we write a program that prints even numbers…
This video teaches viewers about errors in exception handling.

912 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

20 Experts available now in Live!

Get 1:1 Help Now