Declare exceptions using the throws keyword

/*
There are two ways to handle exceptions:
1. Declare throws throws
2. Catch, try...catch.. The following program demonstrates the first way: declare throws, use the throws keyword to throw upwards at the position of the method life Exception */ import java.io.*; public class fuck2{ //public static void main(String[] args)thorws FileNotFoundException { //public static void main(String[] args)thorws IOException { public static void main( String[] args) thorws Exception { //Create a file input stream, read the file //Think: How does the java compiler know that an exception may occur during the execution of the following code //How does the java compiler know the probability of this exception occurring It is relatively high //java compiler is not so smart, because the constructor of FileInputStream //throws FileNotFoundException is used in the declared position FileInputStream fls=new FileInputStream("c:/ab.txt"); } } /*





























The above program does not compile through
fuck2.java:16: error: unreported exception error FileNotFoundException; must be caught or declared to throw
                FileInputStream fls=new FileInputStream("c:/ab.txt");
                                    
*/

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325896008&siteId=291194637