Java StackTraceElement getFileName() 方法



描述

Java StackTraceElement getFileName() 方法返回包含此堆疊跟蹤元素表示的執行點的原始檔的名稱。

宣告

以下是 Java StackTraceElement getFileName() 方法的宣告

public String getFileName()

引數

返回值

此方法返回包含此堆疊跟蹤元素表示的執行點的檔案的名稱,如果此資訊不可用,則返回 null。

異常

示例:使用 StackTraceElement 獲取當前執行緒檔名

以下示例演示了 Java StackTraceElement getFileName() 方法的使用。在此程式中,我們定義了一個名為 function2() 的函式,該函式從當前執行緒的 StackTraceElement 獲取檔名。另一個名為 function1() 的函式用於例項化 StackTraceElementDemo 類並呼叫 function2() 方法。在 main 方法中,我們呼叫了 function1() 方法並列印結果。

package com.tutorialspoint;

public class StackTraceElementDemo {

   // main method
   public static void main(String[] args) {
      // call function1() to invoke function2()     
      function1();
   }
   
   // function1() is to invoke function2()
   // using StackTraceElementDemo object
   public static void function1() {
      new StackTraceElementDemo().function2();
   }
 
   // print the file name of current thread using StackTraceElement
   public void function2() {
      System.out.print("File name : ");
      System.out.print(Thread.currentThread().getStackTrace()[1].
      getFileName());
   }
}

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

File name : StackTraceElementDemo.java

示例:使用 ArithmeticException 的 StackTraceElement 獲取檔名

以下示例演示了 Java StackTraceElement getFileName() 方法的使用。在 main 方法中,我們建立了三個 int 變數,其中一個變數的值為零。在 try-catch 塊中,我們建立了一個除以零的情況以引發 ArithmeticException。在 catch 塊中,我們處理了 ArithmeticException,使用 getStackTrace() 方法從中檢索了一個 StackTraceElement 陣列,然後在陣列的第一個元素上使用 getFileName() 方法,列印檔名。

package com.tutorialspoint;

public class StackTraceElementDemo {

   public static void main(String[] args) {

      // variables to store int values
      int a = 0, b = 1, c;

      try {
         // being divide by zero, this line will throw an ArithmeticException
         c = b / a;
      }catch(ArithmeticException e) {
         // get the array of StackTraceElement
         StackTraceElement[] stackTraceElements =  e.getStackTrace();
         // print the file name
         System.out.println(stackTraceElements[0].getFileName());
      }
   }
}  

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

StackTraceElementDemo.java

示例:使用 Exception 的 StackTraceElement 獲取檔名

以下示例演示了 Java StackTraceElement getFileName() 方法的使用。在 main 方法中,我們建立了三個 int 變數,其中一個變數的值為零。在 try-catch 塊中,我們建立了一個除以零的情況以引發 ArithmeticException。在 catch 塊中,我們處理了 Exception,使用 getStackTrace() 方法從中檢索了一個 StackTraceElement 陣列,然後在陣列的第一個元素上使用 getFileName() 方法,列印檔名。

package com.tutorialspoint;

public class StackTraceElementDemo {

   public static void main(String[] args) {

      // variables to store int values
      int a = 0, b = 1, c;

      try {
         // being divide by zero, this line will throw an ArithmeticException
         c = b / a;
      }catch(Exception e) {
         // get the array of StackTraceElement
         StackTraceElement[] stackTraceElements =  e.getStackTrace();
         // print the file name
         System.out.println(stackTraceElements[0].getFileName());
      }
   }
}  

輸出

讓我們編譯並執行上述程式,這將產生以下結果:

StackTraceElementDemo.java
java_lang_stacktraceelement.htm
廣告