StackTraceElement StackTraceElement(String, String, String, int) in Java

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

Syntax:

java.lang.StackTraceElement.StackTraceElement(String declaringClass, String methodName, String fileName, int lineNumber)

This method takes four arguments. 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.

Parameters: Four parameters are required for this method.

declaringClass: the fully qualified name of the class containing the execution point represented by the stack trace element.

methodName: the name of the method containing the execution point represented by the stack trace element.

fileName: the name of the file containing the execution point represented by the stack trace element, or null if this information is unavailable.

lineNumber: the line number of the source line containing the execution point represented by this stack trace element, or a negative number if this information is unavailable. A value of -2 indicates that the method containing the execution point is a native method.

Throws:

1. NullPointerException - if declaringClass or methodName is null.

Approach 1: When no exception

Java

package com.StackTraceElement;

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

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

        System.out.println(stackTraceElement);
    }
}

Output:

java.lang.String.length()(String.class:675)


Approach 2: NullPointerException

Java

package com.StackTraceElement;

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

        String declaringClass = "java.lang.String";
        String methodName = null;
        String fileName = "String.class";
        int lineNumber = 675;
        StackTraceElement stackTraceElement =
new StackTraceElement(declaringClass,
methodName, fileName, lineNumber);

        System.out.println(stackTraceElement);
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Method name is null at java.base/java.util.Objects.requireNonNull(Objects.java:233) at java.base/java.lang.StackTraceElement.<init>(StackTraceElement.java:141) at java.base/java.lang.StackTraceElement.<init>(StackTraceElement.java:98) at com.StackTraceElement.StackTraceElement1.main(StackTraceElement1.java:10)


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