📜  Java中的可抛出 addSuppressed() 方法及示例

📅  最后修改于: 2022-05-13 01:54:33.950000             🧑  作者: Mango

Java中的可抛出 addSuppressed() 方法及示例

Throwable 类的addSuppressed?(Throwable 异常)方法用于将异常附加到被抑制的异常以传递此异常。此方法是线程安全的方法。此方法通常由 try-catch 子句调用。 Throwable 的抑制行为是启用的,除非通过构造函数禁用,并且当禁用抑制时,此方法除了验证其参数外什么也不做。
在 try-finally 块的默认行为中,我们有两个异常,原始异常被抑制并显示来自 finally 块的异常。在某些情况下,finally 块用于关闭资源,在这种情况下,我们希望看到原始异常,以及来自 final 块的一些异常,它关闭了我们的资源并失败了,因此我们可以添加那些通过抑制的异常这种方法。
如果有多个同级异常并且只能传播一个,则可以使用此方法传播该方法。
句法:

public final void addSuppressed?(Throwable exception)

参数:此方法仅接受一个参数异常,我们希望将其添加为抑制异常。
返回:此方法不返回任何内容。
异常:此方法引发以下异常:

  • IllegalArgumentException :如果异常是可抛出的; throwable 无法抑制自身。
  • NullPointerException :如果异常为空。

下面的程序说明了 addSuppressed?() 方法:
示例 1:

Java
// Java program to demonstrate
// the addSuppressed() Method.
 
import java.io.*;
 
class GFG {
 
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
 
        try {
 
            testException();
        }
 
        catch (Throwable e) {
 
            // get StackTraceElements
            Throwable[] suppExe
                = e.getSuppressed();
 
            // print element of suppExe
            for (int i = 0; i < suppExe.length; i++) {
 
                System.out.println("Suppressed Exceptions:");
                System.out.println(suppExe[i]);
            }
        }
    }
 
    // method which throws Exception
    public static void testException()
        throws Exception
    {
 
        // creating a suppressed Exception
        Exception
            suppressed
            = new ArithmeticException();
 
        // creating a IOException object
        final Exception exe = new Exception();
 
        // adding suppressed Exception
        // using addSuppressed method
        exe.addSuppressed(suppressed);
 
        // throwing IOException
        throw exe;
    }
}


Java
// Java program to demonstrate
// the addSuppressed() Method.
 
import java.io.*;
 
class GFG {
 
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
 
        try {
 
            testException();
        }
 
        catch (Throwable e) {
 
            // get StackTraceElements
            Throwable[] suppExe
                = e.getSuppressed();
 
            System.out.println("Suppressed Exceptions:");
 
            // print element of suppExe
            for (int i = 0; i < suppExe.length; i++) {
 
                System.out.println(suppExe[i]);
            }
        }
    }
 
    // method which throws Exception
    public static void testException()
        throws Exception
    {
 
        // creating a IOException object
        final Exception exe = new Exception();
 
        // adding suppressed Exception
        // using addSuppressed method
        exe.addSuppressed(new ArithmeticException());
        exe.addSuppressed(new IndexOutOfBoundsException());
        exe.addSuppressed(new ClassNotFoundException());
        exe.addSuppressed(new IOException());
 
        // throwing IOException
        throw exe;
    }
}


Java
// Java program to demonstrate
// Exception IllegalArgumentException
// the addSuppressed() Method.
 
import java.io.*;
 
class GFG {
 
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
 
        try {
 
            ArithmeticException
                e
                = new ArithmeticException();
            e.addSuppressed(e);
        }
 
        catch (Throwable e) {
            System.out.println("Exception:" + e);
        }
    }
}


Java
// Java program to demonstrate
// Exception NullPointerException
// the addSuppressed() Method.
 
import java.io.*;
 
class GFG {
 
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
 
        try {
 
            ArithmeticException
                e
                = new ArithmeticException();
            e.addSuppressed(null);
        }
 
        catch (Throwable e) {
            System.out.println("Exception:" + e);
        }
    }
}


输出:
Suppressed Exceptions:
java.lang.ArithmeticException

示例 2:

Java

// Java program to demonstrate
// the addSuppressed() Method.
 
import java.io.*;
 
class GFG {
 
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
 
        try {
 
            testException();
        }
 
        catch (Throwable e) {
 
            // get StackTraceElements
            Throwable[] suppExe
                = e.getSuppressed();
 
            System.out.println("Suppressed Exceptions:");
 
            // print element of suppExe
            for (int i = 0; i < suppExe.length; i++) {
 
                System.out.println(suppExe[i]);
            }
        }
    }
 
    // method which throws Exception
    public static void testException()
        throws Exception
    {
 
        // creating a IOException object
        final Exception exe = new Exception();
 
        // adding suppressed Exception
        // using addSuppressed method
        exe.addSuppressed(new ArithmeticException());
        exe.addSuppressed(new IndexOutOfBoundsException());
        exe.addSuppressed(new ClassNotFoundException());
        exe.addSuppressed(new IOException());
 
        // throwing IOException
        throw exe;
    }
}
输出:
Suppressed Exceptions:
java.lang.ArithmeticException
java.lang.IndexOutOfBoundsException
java.lang.ClassNotFoundException
java.io.IOException

示例 3:显示 IllegalArgumentException

Java

// Java program to demonstrate
// Exception IllegalArgumentException
// the addSuppressed() Method.
 
import java.io.*;
 
class GFG {
 
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
 
        try {
 
            ArithmeticException
                e
                = new ArithmeticException();
            e.addSuppressed(e);
        }
 
        catch (Throwable e) {
            System.out.println("Exception:" + e);
        }
    }
}
输出:
Exception:java.lang.IllegalArgumentException:
 Self-suppression not permitted

示例 4:显示 NullPointerException

Java

// Java program to demonstrate
// Exception NullPointerException
// the addSuppressed() Method.
 
import java.io.*;
 
class GFG {
 
    // Main Method
    public static void main(String[] args)
        throws Exception
    {
 
        try {
 
            ArithmeticException
                e
                = new ArithmeticException();
            e.addSuppressed(null);
        }
 
        catch (Throwable e) {
            System.out.println("Exception:" + e);
        }
    }
}
输出:
Exception:java.lang.NullPointerException:
 Cannot suppress a null exception.

参考:
https://docs.oracle.com/javase/10/docs/api/java Java Java)