Java self - Throwable exception handling

Java Throwable类

Step 1: Throwable

Throwable is the class, Exception and Error inherit this class
so when captured, can be used to capture Throwable be
shown: abnormal points Error and Exception
Exception in divided runtime exception and can be checked exception .
Throwable

package exception;
 
import java.io.File;
import java.io.FileInputStream;
 
public class TestException {
 
    public static void main(String[] args) {
 
        File f = new File("d:/LOL.exe");
 
        try {
            new FileInputStream(f);
            //使用Throwable进行异常捕捉
        } catch (Throwable t) {
            // TODO Auto-generated catch block
            t.printStackTrace();
        }
 
    }
}

Exercise : Throwable class

In the method declaration, you can throw the specified exceptions, such as FileNotFoundException
you can throw Throwable class?

The caller of this method is what should we do?

The answer :

You can throw Throwable, and must be a catch handler at the time of the call.

But this design method to do well, because they do not know the type of throw in the end what kind of specific issues, can not be targeted treatment.

package exception;
 
import java.io.File;
import java.io.FileInputStream;
 
public class TestException {
 
    public static void method() throws Throwable {
        File f = new File("d:/LOL.exe");
        new FileInputStream(f);
    }
 
    public static void main(String[] args) {
        try {
            method();
        } catch (Throwable e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}

Guess you like

Origin www.cnblogs.com/jeddzd/p/11684572.html
Recommended