📜  Java中 throw 和 throws 的区别

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

Java中 throw 和 throws 的区别

先决条件: Java中的 Throw 和 Throws

throw 和 throws 是Java中异常处理的概念,其中 throw 关键字从方法或代码块中显式抛出异常,而 throws 关键字用于方法的签名中。

Java中 throw 和 throws 的区别在于:

S. No.

Key Difference

throw

throws

1.Point of UsageThe throw keyword is used inside a function. It is used when it is required to throw an Exception logically.The throws keyword is used in the function signature. It is used when the function has some statements that can lead to exceptions.
2.Exceptions ThrownThe throw keyword is used to throw an exception explicitly. It can throw only one exception at a time.The throws keyword can be used to declare multiple exceptions, separated by a comma. Whichever exception occurs, if matched with the declared ones, is thrown automatically then.
3.SyntaxSyntax of throw keyword includes the instance of the Exception to be thrown. Syntax wise throw keyword is followed by the instance variable.Syntax of throws keyword includes the class names of the Exceptions to be thrown. Syntax wise throws keyword is followed by exception class names.
4.Propagation of Exceptionsthrow keyword cannot propagate checked exceptions. It is only used to propagate the unchecked Exceptions that are not checked using the throws keyword. throws keyword is used to propagate the checked Exceptions only. 

例子

Java抛出

Java
// Java program to demonstrate the working 
// of throw keyword in exception handling
  
public class GFG {
    public static void main(String[] args)
    {
        // Use of unchecked Exception
        try {
            // double x=3/0;
            throw new ArithmeticException();
        }
        catch (ArithmeticException e) {
            e.printStackTrace();
        }
    }
}


Java
// Java program to demonstrate the working
// of throws keyword in exception handling
import java.io.*;
import java.util.*;
  
public class GFG {
  
    public static void writeToFile() throws Exception
    {
        BufferedWriter bw = new BufferedWriter(
            new FileWriter("myFile.txt"));
        bw.write("Test");
        bw.close();
    }
  
    public static void main(String[] args) throws Exception
    {
        try {
            writeToFile();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}


输出:

java.lang.ArithmeticException
    at GFG.main(GFG.java:10)

2. Java抛出

Java

// Java program to demonstrate the working
// of throws keyword in exception handling
import java.io.*;
import java.util.*;
  
public class GFG {
  
    public static void writeToFile() throws Exception
    {
        BufferedWriter bw = new BufferedWriter(
            new FileWriter("myFile.txt"));
        bw.write("Test");
        bw.close();
    }
  
    public static void main(String[] args) throws Exception
    {
        try {
            writeToFile();
        }
        catch (Exception e) {
            e.printStackTrace();
        }
    }
}

输出:

java.security.AccessControlException: access denied ("java.io.FilePermission" "myFile.txt" "write")
  at GFG.writeToFile(GFG.java:10)