Java self - Exception processing

Java exception handling approach to try catch throws

Exception handling common means: try catch finally throws

Step. 1: the try the catch

1. FileNotFoundException might be thrown exception file does not exist in the code in try Lane
2. If the file exists, execution continues down the catch block and the code is not performed

  1. If the file does not exist, the code in the try will immediately terminate, program flow runs into the corresponding catch block
  2. e.printStackTrace (); trace invocation prints out the method, in this example, line 16 will print abnormal begins TestException, thus facilitating the positioning and in the end, where is the abnormality analysis
    try catch

    package exception;

    import java.io.File;
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;

    public class TestException {

     public static void main(String[] args) {
    
         File f= new File("d:/LOL.exe");
    
         try{
             System.out.println("试图打开 d:/LOL.exe");
             new FileInputStream(f);
             System.out.println("成功打开");
         }
         catch(FileNotFoundException e){
             System.out.println("d:/LOL.exe不存在");
             e.printStackTrace();
         }
    
     }

    }

Step 2: Use the parent classes abnormality catch

Exception is FileNotFoundException subclass may be used Exception catch live FileNotFoundException

package exception;
  
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
  
public class TestException {
  
    public static void main(String[] args) {
          
        File f= new File("d:/LOL.exe");
          
        try{
            System.out.println("试图打开 d:/LOL.exe");
            new FileInputStream(f);
            System.out.println("成功打开");
        }
         
        catch(Exception e){
            System.out.println("d:/LOL.exe不存在");
            e.printStackTrace();
        }
          
    }
}

Step 3: more than 1 way to capture abnormal

Sometimes a piece of code will throw a variety of exceptions, such as

new FileInputStream(f);
Date d = sdf.parse("2016-06-03");

This code will throw an exception ParseException a FileNotFoundException and parse the file does not exist
, one solution is to catch separately

package exception;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class TestException {
 
    public static void main(String[] args) {
 
        File f = new File("d:/LOL.exe");
 
        try {
            System.out.println("试图打开 d:/LOL.exe");
            new FileInputStream(f);
            System.out.println("成功打开");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = sdf.parse("2016-06-03");
        } catch (FileNotFoundException e) {
            System.out.println("d:/LOL.exe不存在");
            e.printStackTrace();
        } catch (ParseException e) {
            System.out.println("日期格式解析错误");
            e.printStackTrace();
        }
    }
}

Step 4: multi-way abnormal capture 2

Another approach is to a plurality of kinds of abnormalities, in a unified capture catch in
catch (FileNotFoundException | ParseException e) { }

This mode is supported from JDK7, is to capture the benefits of the code is more compact , the downside is that once the exception, not sure what kind of abnormality in the end , you need to determine the specific type of exception by instanceof

package exception;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
 
public class TestException {
 
    public static void main(String[] args) {
 
        File f = new File("d:/LOL.exe");
 
        try {
            System.out.println("试图打开 d:/LOL.exe");
            new FileInputStream(f);
            System.out.println("成功打开");
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
            Date d = sdf.parse("2016-06-03");
        } catch (FileNotFoundException | ParseException e) {
            if (e instanceof FileNotFoundException)
                System.out.println("d:/LOL.exe不存在");
            if (e instanceof ParseException)
                System.out.println("日期格式解析错误");
 
            e.printStackTrace();
        }
 
    }
}

Step 5: a finally

Whether or not an exception, finally the code will be executed

package exception;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
 
public class TestException {
 
    public static void main(String[] args) {
         
        File f= new File("d:/LOL.exe");
         
        try{
            System.out.println("试图打开 d:/LOL.exe");
            new FileInputStream(f);
            System.out.println("成功打开");
        }
        catch(FileNotFoundException e){
            System.out.println("d:/LOL.exe不存在");
            e.printStackTrace();
        }
        finally{
            System.out.println("无论文件是否存在, 都会执行的代码");
        }
    }
}

Step 6: throws

Consider the following situation:
the main method call method1
method1 calls method2
method2 open the file

method2 in the need for exception handling
but method2 not going to deal with , but the exception is thrown throws to go through
so method1 will receive the exception . Approach is two kinds, either try catch disposed of, or is thrown out .
Select Local try catch method1 try catch live once lived, is equivalent to the exception digested, the main method when calling method1, you do not need to deal with abnormal

package exception;
 
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
 
public class TestException {
 
    public static void main(String[] args) {
        method1();
 
    }
 
    private static void method1() {
        try {
            method2();
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
 
    }
 
    private static void method2() throws FileNotFoundException {
 
        File f = new File("d:/LOL.exe");
 
        System.out.println("试图打开 d:/LOL.exe");
        new FileInputStream(f);
        System.out.println("成功打开");
 
    }
}

Step 7: the difference between the throw and throws

throws and throw these two keywords close, but the meaning is not the same, with the following differences:

  1. throws appeared on the method declaration, but usually throw in vivo appear.
  2. throws represents a possibility of abnormal, not necessarily these anomalies occur; throw an exception is thrown, then the execution must throw an exception thrown objects.

Exercise : Exception Handling

Suppose you have a method public int method (), returns an integer
have try catch and finally in this method.
The try in return 1
the catch in return 2
finally returns 3 years
then this method returns in the end how much?

The answer :

package exception;
 
public class TestException {
 
    public static int method() {
        try {
            return return1();
        } catch (Exception e) {
            return return2();
        } finally {
            return return3();
        }
    }
 
    private static int return1() {
        System.out.println("return 1");
        return 1;
    }
    private static int return2() {
        System.out.println("return 2");
        return 2;
    }
    private static int return3() {
        System.out.println("return 3");
        return 3;
    }
 
    public static void main(String[] args) {
        int result = method();
        System.out.println("result:" + result);
         
    }
}

We can see, the final result is finally taking the return value of 3.
Meanwhile, deliberately designed as a method call return1 (), return2 (), return3 ().
It can be observed, try in return and finally returns are executed, but the final choice of values returned finally in the use

Guess you like

Origin www.cnblogs.com/jeddzd/p/11664724.html