StackTraceElement.getLineNumber() Method



Description

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

Declaration

Following is the declaration for java.lang.StackTraceElement.getLineNumber() method

public int getLineNumber()

Parameters

NA

Return Value

This method returns 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.

Exception

NA

Example

The following example shows the usage of java.lang.StackTraceElement.getLineNumber() method.

package com.tutorialspoint;

import java.lang.*;

public class StackTraceElementDemo {

   public static void main(String[] args) {
      function1();
   }
 
   public static void function1() {
      new StackTraceElementDemo().function2();
   }

   public void function2() {
      System.out.print("line number : ");
      System.out.print(Thread.currentThread().getStackTrace()[1].
      getLineNumber());
   }
}

Let us compile and run the above program, this will produce the following result −

line number : 17
java_lang_stacktraceelement.htm
Advertisements