StackTraceElement toString() in Java

toString(): This method is available in the java.lang.StackTraceElement class of Java.

Syntax:

String java.lang.StackTraceElement.toString()

This method returns a string representation of this stack trace element.

Parameters: NA

Returns: a string representation of the object.

Exceptions: NA

Approach

Java

package com.StackTraceElement;

public class StackTraceElementtoString {
    public static void main(String[] args) {

        String classLoaderName = "java.lang.String";
        String moduleName = "java.base";
        String moduleVersion = "1.0";
        String declaringClass = "java.lang.String";
        String methodName = "length()";
        String fileName = "String.class";
        int lineNumber = 675;
        StackTraceElement stackTraceElement =
new StackTraceElement(classLoaderName,
moduleName, moduleVersion,
                declaringClass, methodName, fileName, lineNumber);

        System.out.println(stackTraceElement.toString());
    }
}

Output:

java.lang.String/java.base@1.0/java.lang.String.length()(String.class:675)


Some other methods of StackTraceElement

StackTraceElement(String, String, String, int)This method creates a stack trace element representing the specified execution point. The module name and module version of the stack trace element will be null.

StackTraceElement(String, String, String, String, String, String, int)This method creates a stack trace element representing the specified execution point.

equals(Object)This method returns true if the specified object is another StackTraceElement instance representing the same execution point as this instance.

getClassLoaderName()This method returns the name of the class loader of the class containing the execution point represented by this stack trace element.

getClassName()This method returns the fully qualified name of the class containing the execution point represented by this stack trace element.

getFileName()This method returns the name of the source file containing the execution point represented by this stack trace element.

getLineNumber()This method returns the line number of the source line containing the execution point represented by this stack trace element.

getMethodName()This method returns the name of the method containing the execution point represented by this stack trace element.

getModuleName()This method returns the module name of the module containing the execution point represented by this stack trace element.

getModuleVersion()This method returns the module version of the module containing the execution point represented by this stack trace element.

hashCode()This method returns a hash code value for this stack trace element.

isNativeMethod()This method returns true if the method containing the execution point represented by this stack trace element is a native method.

toString()This method returns a string representation of this stack trace element.

No comments:

Post a Comment