Java Journal Day 34 2020.8.8

abnormal

exception handling

throws

import java.io.FileNotFoundException;

public class Demo01Throws {

    public static void main(String[] args) throws FileNotFoundException {
        readFile("C:\\b.txt");
    }

    public static void readFile(String name) throws FileNotFoundException {
        if(name != "C:\\a.txt"){
            throw new FileNotFoundException();
        }
        System.out.println("The filename is correct!");
    }
}

 

try...catch

import java.io.FileNotFoundException;

public class Demo01Throws {

    public static void main(String[] args) {
        try {
            //Write code that may throw errors
            readFile("C:\\b.txt");
        } catch (FileNotFoundException e) {
            System.out.println(e);
        }
    }

    public static void readFile(String name) throws FileNotFoundException {
        if(name != "C:\\a.txt"){
            throw new FileNotFoundException();
        }
        System.out.println("The filename is correct!");
    }
}

 

Three exception handling methods in the Throwable class

1. public String getMessage() Returns a short description of this throwable

import java.io.FileNotFoundException;

public class Demo01Throws {

    public static void main(String[] args) {
        try {
            //Write code that may throw errors
            readFile("C:\\b.txt");
        } catch (FileNotFoundException e) {
            System.out.println(e.getMessage());//back to this throwable short description of
        }
        System.out.println("follow-up code");
    }

    public static void readFile(String name) throws FileNotFoundException {
        if(name != "C:\\a.txt"){
            throw new FileNotFoundException("Filename is wrong!");
        }
        System.out.println("The filename is correct!");
    }
}

 

2. String toString() returns the detailed message string of this throwable

import java.io.FileNotFoundException;

public class Demo01Throws {

    public static void main(String[] args) {
        try {
            //Write code that may throw errors
            readFile("C:\\b.txt");
        } catch (FileNotFoundException e) {
            System.out.println(e.toString());//back to this throwable Detail message string for
        }
        System.out.println("follow-up code");
    }

    public static void readFile(String name) throws FileNotFoundException {
        if(name != "C:\\a.txt"){
            throw new FileNotFoundException("Filename is wrong!");
        }
        System.out.println("The filename is correct!");
    }
}

The result is as follows:

3. void printStackTrace() JVM prints the exception object. By default, this method is used to print the most comprehensive exception information.

import java.io.FileNotFoundException;

public class Demo01Throws {

    public static void main(String[] args) {
        try {
            //Write code that may throw errors
            readFile("C:\\b.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();//JVM Print the exception object, this method is the default, and the printed exception information is the most comprehensive
        }
        System.out.println("follow-up code");
    }

    public static void readFile(String name) throws FileNotFoundException {
        if(name != "C:\\a.txt"){
            throw new FileNotFoundException("Filename is wrong!");
        }
        System.out.println("The filename is correct!");
    }
}

The result is as follows:

 

 

finally block

The code in the finally block is executed regardless of whether an exception occurs or not

Precautions:

1.finally cannot be used alone, it must be used together with try

2.finally is generally used for resource release (resource recovery), no matter whether the program is abnormal or not, the resource release (IO) is finally required.

import java.io.FileNotFoundException;

public class Demo01Throws {

    public static void main(String[] args) {
        try {
            //Write code that may throw errors
            readFile("C:\\b.txt");
        } catch (FileNotFoundException e) {
            e.printStackTrace();//JVM Print the exception object, this method is the default, and the printed exception information is the most comprehensive
        } finally {
            System.out.println("Resource release");
        }
        System.out.println("follow-up code");
    }

    public static void readFile(String name) throws FileNotFoundException {
        if(name != "C:\\a.txt"){
            throw new FileNotFoundException("Filename is wrong!");
        }
        System.out.println("The filename is correct!");
    }
}

 

 

 

This time the content has actually been learned, but the feeling that time was very confusing, this time is better.

*Question: What's the difference between throws and throws, other than the location?

  throw

1. throw is a statement that throws an exception, usually inside a code block, when the program

When a logic error occurs, the programmer actively throws a specific type of exception

2. Defined in the body of the method

3. An exception object is created

4. Determine which exception occurs before it can be used

 throws

1. After the method parameter list, throws can be followed by multiple exception names, indicating that the thrown exceptions are separated by commas

2. Indicates that an exception is thrown to the location where the class is called, and it is not resolved in this class

3. What kind of abnormality may occur

 

throws is used after the method declaration, followed by the exception class name, throw is used in the method body, followed by the exception object name.

throws can be followed by multiple exception class names, separated by commas, and throw can only throw one exception object name.

throws means throwing an exception, which is handled by the caller of the method, and throw means throwing an exception, which is handled by the statement in the method body.

throws represents a possibility of exceptions, and these exceptions do not necessarily occur. Throw means that an exception is thrown, and executing throw must throw some kind of exception.

 

Tomorrow's task:

1. Learn all the abnormal knowledge

2. Complete the abnormal practice questions

3. Preview Chapter 12 "Data Processing with I/O"

Posted by getgray on Tue, 24 May 2022 01:50:57 +0300