Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Getting the name of the current method

Status
Not open for further replies.

odenc

Programmer
Sep 16, 2005
9
IE
Hi all,
I am using JDK 1.3.1 and want to get the name of the current method as a string (for exception handling).
Does anybody know how to do this?
Thanks,
Caroline.
 
In JDK 1.4, the best way would be :

Code:
		String methodData = "";
		try {
			throw new Exception();
		} catch (Exception e) {
			StackTraceElement[] ste = e.getStackTrace();
			methodData = ste[0].getClassName() +"." +ste[0].getMethodName() +"(line:" +ste[0].getLineNumber() +")";
		}

		System.out.println(methodData);

However, the StackTraceElement does not exist in JDK 1.3 ...

so in JDK 1.3, I would do something like :

Code:
		String stackTrace = "";
		try {
			throw new Exception();
		} catch (Exception e) {
			ByteArrayOutputStream baos = new ByteArrayOutputStream();
			PrintWriter ps = new PrintWriter(baos, true);
			e.printStackTrace(ps);
			ps.close();

			stackTrace = new String(baos.toByteArray());
		}

		// You may want to refine this :
		String methodData = stackTrace.substring(stackTrace.indexOf("at ")+3, stackTrace.indexOf(")"));

		System.out.println(methodData);

--------------------------------------------------
Free Java/J2EE Database Connection Pooling Software
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top