最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

JVM處理未捕獲異常的方法詳解

 更新時間:2019年01月07日 09:15:56   作者:技術小黑屋  
這篇文章主要給大家介紹了關于JVM處理未捕獲異常的相關資料,文中通過示例代碼以及圖文介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

前言

繼之前的文章詳解JVM如何處理異常,今天再次發(fā)布一篇比較關聯(lián)的文章,如題目可知,今天聊一聊在JVM中線程遇到未捕獲異常的問題,其中涉及到線程如何處理未捕獲異常和一些內(nèi)容介紹。

什么是未捕獲異常

未捕獲異常指的是我們在方法體中沒有使用try-catch捕獲的異常,比如下面的例子

private static void testUncaughtException(String arg) {
 try {
 System.out.println(1 / arg.length());
 } catch (ArithmeticException e) {
 e.printStackTrace();
 }
}

上面的代碼很有可能發(fā)生如下情況

  • 如果方法參數(shù)arg傳遞null,會出現(xiàn)NullPointerException
  • 如果參數(shù)arg傳遞內(nèi)容為空的字符串(“”),會出現(xiàn)ArithmeticException

對于上面的問題,我們不難發(fā)現(xiàn)

  • 上面可能出現(xiàn)的NullPointerException和ArithmeticException都屬于Unchecked Exceptions
  • 而ArithmeticException被我們?nèi)藶閠ry-catch捕獲了,它不符合本文對于未捕獲異常的定義
  • NullPointerException 由于我們沒有catch住,就變成了我們要聊的未捕獲異常
  • 另外,未捕獲異常實際是Unchecked Exceptions的子集

UncaughtExceptionHandler 是什么

  • 它是線程遇到未捕獲異常的一個處理者接口
  • 它包含一個方法void uncaughtException(Thread t, Throwable e); 用來處理接收處理異常發(fā)生后的操作,比如收集崩潰信息并上報等
  • 可以通過 實例方法 Thread.setUncaughtExceptionHandler 為某一個Thread實例設置未捕獲異常處理者
  • 也可以通過 靜態(tài)方法 Thread.setDefaultUncaughtExceptionHandler 設置所有Thread實例的未捕獲異常處理者

ThreadGroup 是什么

  • ThreadGroup 是線程的集合
  • ThreadGroup 也可以包含子ThreadGroup
  • 除了初始的ThreadGroup 之外,每個ThreadGroup都有一個父 ThreadGroup
  • ThreadGroup 自身實現(xiàn)了Thread.UncaughtExceptionHandler,用來相應處理其內(nèi)部的線程和ThreadGroup發(fā)生未捕獲異常。

未捕獲異常處理者 設置指南

線程發(fā)生了未捕獲異常,JVM怎么處理

分發(fā)Throwable實例

當線程A中出現(xiàn)了未捕獲異常時,JVM會調(diào)用線程A的dispatchUncaughtException(Throwable)方法

/**
 * Dispatch an uncaught exception to the handler. This method is
 * intended to be called only by the JVM.
 */
private void dispatchUncaughtException(Throwable e) {
 getUncaughtExceptionHandler().uncaughtException(this, e);
}

獲取未捕獲異常處理者

每個線程會有一個變量(uncaughtExceptionHandler)來保存未捕獲異常的處理者

在線程需要確定Throwable分發(fā)目標的處理者時,優(yōu)先獲取當前線程中uncaughtExceptionHandler變量

如果出問題線程的uncaughtExceptionHandler為null(即沒有顯式設置異常處理者),則使用自己所在的ThreadGroup來作為未捕獲異常處理者。

/**
 * Returns the handler invoked when this thread abruptly terminates
 * due to an uncaught exception. If this thread has not had an
 * uncaught exception handler explicitly set then this thread's
 * <tt>ThreadGroup</tt> object is returned, unless this thread
 * has terminated, in which case <tt>null</tt> is returned.
 * @since 1.5
 * @return the uncaught exception handler for this thread
 */
public UncaughtExceptionHandler getUncaughtExceptionHandler() {
 return uncaughtExceptionHandler != null ?
 uncaughtExceptionHandler : group;
}

如果Throwable分發(fā)給ThreadGroup

  • ThreadGroup會嘗試轉(zhuǎn)給它的父ThreadGroup(如果存在的話)
  • 如果上面沒有找到對應的ThreadGroup,則嘗試獲取Thread.getDefaultUncaughtExceptionHandler()并分發(fā)
/**
 * Called by the Java Virtual Machine when a thread in this
 * thread group stops because of an uncaught exception, and the thread
 * does not have a specific {@link Thread.UncaughtExceptionHandler}
 * installed.
 * <p>
 * The <code>uncaughtException</code> method of
 * <code>ThreadGroup</code> does the following:
 * <ul>
 * <li>If this thread group has a parent thread group, the
 * <code>uncaughtException</code> method of that parent is called
 * with the same two arguments.
 * <li>Otherwise, this method checks to see if there is a
 * {@linkplain Thread#getDefaultUncaughtExceptionHandler default
 * uncaught exception handler} installed, and if so, its
 * <code>uncaughtException</code> method is called with the same
 * two arguments.
 * <li>Otherwise, this method determines if the <code>Throwable</code>
 * argument is an instance of {@link ThreadDeath}. If so, nothing
 * special is done. Otherwise, a message containing the
 * thread's name, as returned from the thread's {@link
 * Thread#getName getName} method, and a stack backtrace,
 * using the <code>Throwable</code>'s {@link
 * Throwable#printStackTrace printStackTrace} method, is
 * printed to the {@linkplain System#err standard error stream}.
 * </ul>
 * <p>
 * Applications can override this method in subclasses of
 * <code>ThreadGroup</code> to provide alternative handling of
 * uncaught exceptions.
 *
 * @param t the thread that is about to exit.
 * @param e the uncaught exception.
 * @since JDK1.0
 */
 public void uncaughtException(Thread t, Throwable e) {
 if (parent != null) {
  parent.uncaughtException(t, e);
 } else {
  Thread.UncaughtExceptionHandler ueh =
  Thread.getDefaultUncaughtExceptionHandler();
  if (ueh != null) {
  ueh.uncaughtException(t, e);
  } else if (!(e instanceof ThreadDeath)) {
  System.err.print("Exception in thread \""
     + t.getName() + "\" ");
  e.printStackTrace(System.err);
  }
 }
 }

將上面的處理流程做成圖的形式,就是下圖所示

注:上述圖片來自https://www.javamex.com/tutorials/exceptions/exceptions_uncaught_handler.shtml

Questions

初始的ThreadGroup是什么

上面提到了初始的ThreadGroup沒有父ThreadGroup,是主線程所在的ThreadGroup么?

這個問題,我們可以通過這樣一段代碼驗證

private static void dumpThreadGroups() {
 ThreadGroup threadGroup = Thread.currentThread().getThreadGroup();
 while(threadGroup != null) {
 System.out.println("dumpThreadGroups threadGroup=" + threadGroup.getName());
 threadGroup = threadGroup.getParent();
 }
}

執(zhí)行該方法對應的輸出是

dumpThreadGroups threadGroup=main
dumpThreadGroups threadGroup=system

因此我們可以發(fā)現(xiàn),初始的ThreadGroup是一個叫做system的ThreadGroup,而不是main ThreadGroup

setDefaultUncaughtExceptionHandler 設置的一定會被調(diào)用到么

這其實是一個很好的問題,答案是不一定會被調(diào)用,因為可能存在以下的情況

  • 出問題的線程設置了對應的UncaughtExcpetionHandler,優(yōu)先響應分發(fā)到這個Handler
  • 出問題的線程所在的ThreadGroup包括其祖先ThreadGroup 重寫了uncaughtException 也可能造成線程默認的Handler無法被調(diào)用
  • 出問題的線程重寫了dispatchUncaughtException 可能性較小
  • 出問題的線程重寫了getUncaughtExceptionHandler 可能性較小

參考聲明

How uncaught exceptions are handled

總結(jié)

以上就是這篇文章的全部內(nèi)容了,希望本文的內(nèi)容對大家的學習或者工作具有一定的參考學習價值,如果有疑問大家可以留言交流,謝謝大家對腳本之家的支持。

相關文章

  • Java線程同步機制_動力節(jié)點Java學院整理

    Java線程同步機制_動力節(jié)點Java學院整理

    在之前,已經(jīng)學習到了線程的創(chuàng)建和狀態(tài)控制,但是每個線程之間幾乎都沒有什么太大的聯(lián)系??墒怯械臅r候,可能存在多個線程多同一個數(shù)據(jù)進行操作,這樣,可能就會引用各種奇怪的問題?,F(xiàn)在就來學習多線程對數(shù)據(jù)訪問的控制吧
    2017-05-05
  • Java程序生成Access文件代碼實例

    Java程序生成Access文件代碼實例

    這篇文章主要介紹了Java程序生成Access文件代碼實例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友可以參考下
    2019-09-09
  • 當面試官問我ArrayList和LinkedList哪個更占空間時,我是這么答的(面試官必問)

    當面試官問我ArrayList和LinkedList哪個更占空間時,我是這么答的(面試官必問)

    今天介紹一下Java的兩個集合類,ArrayList和LinkedList,這兩個集合的知識點幾乎可以說面試必問的。感興趣的朋友跟隨小編一起看看吧
    2020-08-08
  • ssm項目改造spring?boot項目完整步驟

    ssm項目改造spring?boot項目完整步驟

    Spring?Boot現(xiàn)在已經(jīng)成為Java開發(fā)領域的一顆璀璨明珠,它本身是包容萬象的,可以跟各種技術集成,下面這篇文章主要給大家介紹了關于ssm項目改造spring?boot項目的相關資料,需要的朋友可以參考下
    2023-04-04
  • 基于jdk1.8的Java源碼詳解 Integer

    基于jdk1.8的Java源碼詳解 Integer

    這篇文章主要介紹了基于jdk1.8的Java源碼詳解 Integer,Integer是int的Warpper類,是面向?qū)ο蟮募碠OP的對象類型,,需要的朋友可以參考下
    2019-06-06
  • java實現(xiàn)英文翻譯程序

    java實現(xiàn)英文翻譯程序

    這篇文章主要為大家詳細介紹了java實現(xiàn)英文翻譯程序,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2019-06-06
  • Java使用IO流實現(xiàn)音頻的剪切和拼接

    Java使用IO流實現(xiàn)音頻的剪切和拼接

    這篇文章主要為大家詳細介紹了Java使用IO流實現(xiàn)音頻的剪切和拼接,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2017-06-06
  • springboot v2.0.3版本多數(shù)據(jù)源配置方法

    springboot v2.0.3版本多數(shù)據(jù)源配置方法

    這篇文章主要介紹了springboot v2.0.3版本多數(shù)據(jù)源配置方法,本文通過實例代碼給大家介紹的非常詳細,具有一定的參考借鑒價值 ,需要的朋友可以參考下
    2018-11-11
  • Spring Boot 日志概念及使用詳解

    Spring Boot 日志概念及使用詳解

    文章介紹了日志的重要性、日志格式、日志使用方法、日志級別、日志配置、日志持久化以及使用Lombok簡化日志輸出,感興趣的朋友一起看看吧
    2025-03-03
  • 有關于整體刷新和局部刷新frameset窗口

    有關于整體刷新和局部刷新frameset窗口

    本篇小編為大家介紹有關于整體刷新和局部刷新frameset窗口的方法,希望對有需要的朋友有所幫助。
    2013-04-04

最新評論

兰坪| 信阳市| 蓝山县| 虞城县| 吴旗县| 兰溪市| 马公市| 杭锦后旗| 库伦旗| 兴和县| 乌恰县| 哈密市| 沐川县| 东方市| 保康县| 海伦市| 石泉县| 宁乡县| 宁波市| 文化| 隆林| 汨罗市| 神农架林区| 平遥县| 三亚市| 陵川县| 湘潭县| 恭城| 光泽县| 普陀区| 靖边县| 清远市| 巨鹿县| 天全县| 虎林市| 巫山县| 和林格尔县| 隆昌县| 河曲县| 宿松县| 梁平县|