集册 Java实例教程 返回指定异常的根本原因。

返回指定异常的根本原因。

欢马劈雪     最近更新时间:2020-01-02 10:19:05

496
返回指定异常的根本原因。

/*

 * Copyright (c) 2015-2016 QuartzDesk.com.

 * Licensed under the MIT license (https://opensource.org/licenses/MIT).

 */// from 时 代 J a v a 公 众 号

//package com.nowjava;


import java.sql.SQLException;


public class Main {

    /**

     * Returns the root cause of the specified exception.

     *

     * @param t an exception.

     * @return the root cause.

     */

    public static Throwable getRootCause(Throwable t) {

        Throwable cause = t;


        while (getCause(cause) != null)

            cause = getCause(cause);


        return cause;

    }


    /**

     * Returns the cause of the specified exception.

     *

     * @param t an exception.

     * @return the cause.

     */

    public static Throwable getCause(Throwable t) {

        // SQLException does not use "standard" cause chaining...grrr

        if (t instanceof SQLException) {//时代Java公众号
展开阅读全文