欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Java異常處理 如何跟蹤異常的傳播路徑

 更新時(shí)間:2019年07月31日 14:43:28   作者:衛(wèi)平公  
這篇文章主要介紹了Java異常處理 如何跟蹤異常的傳播路徑,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下

當(dāng)程序中出現(xiàn)異常時(shí),JVM會(huì)依據(jù)方法調(diào)用順序依次查找有關(guān)的錯(cuò)誤處理程序。

可使用printStackTrace 和 getMessage方法了解異常發(fā)生的情況:

printStackTrace:打印方法調(diào)用堆棧。

每個(gè)Throwable類的對(duì)象都有一個(gè)getMessage方法,它返回一個(gè)字串,這個(gè)字串是在Exception構(gòu)造函數(shù)中傳入的,通常讓這一字串包含特定異常的相關(guān)信息。

示例程序

// UsingExceptions.java
// Demonstrating the getMessage and printStackTrace
// methods inherited into all exception classes.
public class PrintExceptionStack {
  public static void main( String args[] )
  {
   try {
     method1();
   }
   catch ( Exception e ) {
     System.err.println( e.getMessage() + "\n" );
     e.printStackTrace();
   }
  }
  public static void method1() throws Exception
  {
   method2();
  }
  public static void method2() throws Exception
  {
   method3();
  }
  public static void method3() throws Exception
  {
   throw new Exception( "Exception thrown in method3" );
  }
}

結(jié)果截圖

以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論