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
Solved

get method and class names

Posted on 1998-02-06
6
153 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
Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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.

 
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: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

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

Suggested Solutions

Title # Comments Views Activity
map related example 6 55
eclipse buid path vs tomcat lib path 10 34
Java: How do I open the default windows program for a given file type 3 39
String array comparison 4 38
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Introduction This article is the second of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers the basic installation and configuration of the test automation tools used by…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

860 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