StackTraceElement(String, String, String, String, String, String, int): This method is available in the java.lang.StackTraceElement class of Java.
Syntax:
java.lang.StackTraceElement.StackTraceElement(String classLoaderName, String moduleName, String moduleVersion, String declaringClass, String methodName, String fileName, int lineNumber)
This method takes 7 arguments. This method creates a stack trace element representing the specified execution point.
Parameters: Seven parameters are required for this method.
classLoaderName: the class loader name if the class loader of the class containing the execution point represented by the stack trace is named; otherwise null.
moduleName: the module name if the class containing the execution point represented by the stack trace is in a named module; otherwise null.
moduleVersion: the module version if the class containing the execution point represented by the stack trace is in a named module that has a version; otherwise null.
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 is null or methodName is null.
Approach 1: When no exception
Java
package com.StackTraceElement;public class StackTraceElement2 {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 = newStackTraceElement(classLoaderName,moduleName,moduleVersion,declaringClass, methodName,fileName, lineNumber);System.out.println(stackTraceElement);}}
Output:
java.lang.String/java.base@1.0/java.lang.String.length()(String.class:675)
Approach 2: NullPointerException
Java
package com.StackTraceElement;public class StackTraceElement2 {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 = null;String fileName = "String.class";int lineNumber = 675;StackTraceElement stackTraceElement = newStackTraceElement(classLoaderName, moduleName,moduleVersion,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 com.StackTraceElement.StackTraceElement2.main(StackTraceElement2.java:13)
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