Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

get method and class names

Posted on 1998-02-06
6
Medium Priority
?
158 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 200 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
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

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.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses

916 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