文章目录
- Java进阶【十三期】:
 - 异常处理
 - 一、异常基本介绍
 - 二、编译异常和运行异常
 - 三、总结
 
- 异常的作用
 - 异常的处理方式
 - 一、JVM默认的处理方式
 - 二、自己处理异常
 - 自己 处理的问题
 
- 三、总结
 
- Throwable 成员方法
 - 抛出异常
 - 总结
 
- 异常练习
 - 自定义异常
 
- File
 - File 三个 构造方法
 - File 常见方法
 - 一、File - 判断获取方法
 - 代码演示
 
- 二、File - 创建和删除方法
 - 代码示例
 
- 三、File - 获取并遍历
 
- File - 综合练习
 - 练习一、
 - 练习二、
 - 练习三、
 - 练习四、
 - 练习五、
 - 练习六、
 
Java进阶【十三期】:
异常处理

一、异常基本介绍

 
二、编译异常和运行异常

 ##
三、总结

异常的作用

异常的处理方式

一、JVM默认的处理方式

 
二、自己处理异常

 
自己 处理的问题

 一问答案:
 
二问答案:
 
 三问答案:
 
 四问答案:
 
三、总结

Throwable 成员方法

 
抛出异常

 
总结

异常练习

 Girlfriend类
package com.exception;
public class Girlfriend {
    private String name;
    private int age;
    public Girlfriend() {
    }
    public Girlfriend(String name, int age) {
        this.name = name;
        this.age = age;
    }
    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }
    /**
     * 设置
     * @param name
     */
    public void setName(String name) throws RuntimeException{
        int len = name.length();
        if (len < 3 || len > 10) {
            throw new RuntimeException();
        } else {
            this.name = name;
        }
    }
    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }
    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        if (age < 18 || age > 40) {
            throw new RuntimeException();
        } else {
            this.age = age;
        }
    }
    public String toString() {
        return "Girlfriend{name = " + name + ", age = " + age + "}";
    }
}
 
测试类
package com.exception;
import java.util.Scanner;
public class ExceptionTest {
    public static void main(String[] MakeFullStack) {
        Girlfriend girlfriend = new Girlfriend();
        Scanner sc = new Scanner(System.in);
        while (true) {
            try {
                System.out.println("请输入女朋友的姓名:");
                String girlName = sc.nextLine();
                girlfriend.setName(girlName);
                System.out.println("请输入女朋友年龄:");
                String string = sc.nextLine();
                int girlAge = Integer.parseInt(string);
                girlfriend.setAge(girlAge);
                break;
            } catch (NumberFormatException e) {
                System.out.println("年龄格式有误!");
            } catch (RuntimeException e) {
                System.out.println("姓名或者年龄不符合规范!");
            }
        }
        System.out.println(girlfriend);
    }
}
 
自定义异常

- 首先我们创建连个自定义异常类,一个是处理名字错误的,一个是处理年龄错误的。
 
NameFormatException 姓名处理鳄类
package com.exception;
public class NameFormatException extends RuntimeException {
    private String message;
    public NameFormatException() {
    }
    public NameFormatException(String message) {
        // 将用户抛出异常传入的错误信息提交给父类
        super(message);
        this.message = message;
    }
    public String toString() {
        return "NameFormatException:" + this.message;
    }
}
 
AgeOutOfBoundsException 年龄处理类
package com.exception;
public class AgeOutOfBoundsException extends RuntimeException {
    private String message;
    public AgeOutOfBoundsException() {
    }
    public AgeOutOfBoundsException(String message) {
        // 将用户抛出异常传入的错误信息提交给父类
        super(message);
        this.message = message;
    }
    public String toString() {
        return "AgeOutOfBoundsException:" + message;
    }
}
 
然后我们的女朋友类中,处理的时候,抛出我们自定义异常处理类即可。
package com.exception;
public class Girlfriend {
    private String name;
    private int age;
    public Girlfriend() {
    }
    public Girlfriend(String name, int age) {
        this.name = name;
        this.age = age;
    }
    /**
     * 获取
     * @return name
     */
    public String getName() {
        return name;
    }
    /**
     * 设置
     * @param name
     */
    public void setName(String name) throws RuntimeException{
        int len = name.length();
        if (len < 3 || len > 10) {
            throw new NameFormatException(len + "格式输入有误");
        } else {
            this.name = name;
        }
    }
    /**
     * 获取
     * @return age
     */
    public int getAge() {
        return age;
    }
    /**
     * 设置
     * @param age
     */
    public void setAge(int age) {
        if (age < 18 || age > 40) {
            throw new AgeOutOfBoundsException(age + "年龄超出范围!");
        } else {
            this.age = age;
        }
    }
    public String toString() {
        return "Girlfriend{name = " + name + ", age = " + age + "}";
    }
}
 
最后,我们的测试类中,捕获异常的时候,同样使用我们的自定义异常处理类
package com.exception;
import java.util.Scanner;
public class ExceptionTest {
    public static void main(String[] MakeFullStack) {
        Girlfriend girlfriend = new Girlfriend();
        Scanner sc = new Scanner(System.in);
        while (true) {
            try {
                System.out.println("请输入女朋友的姓名:");
                String girlName = sc.nextLine();
                girlfriend.setName(girlName);
                System.out.println("请输入女朋友年龄:");
                String string = sc.nextLine();
                int girlAge = Integer.parseInt(string);
                girlfriend.setAge(girlAge);
                break;
            } catch (NumberFormatException e) {
                System.out.println("年龄格式有误!");
            } catch (NameFormatException e) {
                // 输出自定义异常捕获到的错误信息
                e.printStackTrace();
            } catch (AgeOutOfBoundsException e) {
                // 输出自定义异常捕获到的错误信息
                e.printStackTrace();
            }
        }
        System.out.println(girlfriend);
    }
}
 
File
文件的路径
File 三个 构造方法

 代码示例:
 
 小结
 
File 常见方法
一、File - 判断获取方法

代码演示
package com.file;
import java.io.File;
public class FileDemo1 {
    public static void main(String [] MakeFullStack) {
        /**
         * File的常见成员方法(判断、获取)
         */
        // 1. isDirectory() 判断 该路径 File 是否为文件夹
        File f11 = new File("E:\\MakeFullStack.txt");
        boolean f11Director = f11.isDirectory();
        System.out.println(f11Director);  // false
        
        File f12 = new File("E:\\MakeItPossible");
        boolean f12Directory = f12.isDirectory();
        System.out.println(f12Directory);  // true
        System.out.println("=====================分界线=====================");
        // 2. isFile() 判断该路径 File 是否为文件
        File f21 = new File("E:\\MakeFullStack.txt");
        boolean f21File = f21.isFile();
        System.out.println(f21File);  // true
        File f22 = new File("E:\\MakeItPossible");
        boolean f22File = f22.isFile();
        System.out.println(f22File);  // false
        System.out.println("=====================分界线=====================");
        // 3. exists () 判断该 路径 File 是否存在
        File f31 = new File("E:\\MakeFullStack.txt");
        boolean f31Exists = f31.exists();
        System.out.println(f31Exists); // true
        File f32 = new File("E:\\MakeItPossible");
        boolean f32Exists = f32.exists();
        System.out.println(f32Exists);  // true
        System.out.println("=====================分界线=====================");
        // 4. length() 返回文件的大小(字节数量)
        File f41 = new File("E:\\MakeFullStack.txt");
        long f41Length = f41.length();
        System.out.println(f41Length);  //  13
        File f42 = new File("E:\\MakeFullStack");
        long f42Length = f42.length();
        System.out.println(f42Length);  //  0
        System.out.println("=====================分界线=====================");
        // 5. getAbsolutePath() 返回文件的绝对路径
        File f51 = new File("E:\\MakeFullStack.txt");
        String f51Path = f51.getAbsolutePath();
        System.out.println(f51Path);  // E:\MakeFullStack.txt
        File f52 = new File("E:\\MakeItPossible");
        String f52Path = f52.getAbsolutePath();
        System.out.println(f52Path);  // E:\MakeItPossible
        System.out.println("=====================分界线=====================");
        // 6. getPath() 返回文件使用的路径
        File f61 = new File("E:\\MakeFullStack.txt");
        String f61Path = f61.getPath();
        System.out.println(f61Path);  // E:\MakeFullStack.txt
        File f62 = new File("E:\\MakeItPossible");
        String f62Path = f62.getPath();
        System.out.println(f62Path);  // E:\MakeItPossible
        System.out.println("=====================分界线=====================");
        // 7. getName() 返回文件的名称,带后缀
        File f71 = new File("E:\\MakeFullStack.txt");
        String f71Name = f71.getName();
        System.out.println(f71Name);  // MakeFullStack.txt
        File f72 = new File("E:\\MakeItPossible");
        String f72Name = f72.getName();
        System.out.println(f72Name);  // MakeItPossible (这是个文件,没以后后缀)
        System.out.println("=====================分界线=====================");
        // 8. lastModified() 返回文件最后的修改时间(时间戳毫秒)
        File f81 = new File("E:\\MakeFullStack.txt");
        long f81LatsTime = f81.lastModified();
        System.out.println(f81LatsTime);  // 1714669081566
        System.out.println("=====================分界线=====================");
    }
}
 
二、File - 创建和删除方法

代码示例
package com.file;
import java.io.File;
import java.io.IOException;
public class FileDemo2 {
    public static void main(String[] MakeFullStack) throws IOException {
        /**
         * File 的常见成员方法(创建、删除)
         */
        // 1. createNewFile() 创建一个新的空文件夹
        File f1 = new File("E:\\MakeCode.txt");
        boolean newFile = f1.createNewFile();
        System.out.println(newFile);  // true
        System.out.println("============================================");
        // 2. mkdir() 创建单级文件夹
        File f2 = new File("E:\\aaa");
        boolean mkdir = f2.mkdir();
        System.out.println(mkdir);  // true
        System.out.println("============================================");
        // 3. mkdirs() 创建多级文件夹
        File f3 = new File("E:\\BBB\\CCC");
        boolean mkdirs = f3.mkdirs();
        System.out.println(mkdirs);   // true
        System.out.println("============================================");
        // 4. delete() 删除文件、空文件夹
        File f4 = new File("E:\\aaa");
        boolean delete = f4.delete();
        System.out.println(delete);  // true
        File f5 = new File("E:\\MakeCode");
        boolean delete1 = f5.delete();
        System.out.println(delete1);  // true
    }
}
 
三、File - 获取并遍历


 
File - 综合练习
练习一、

练习二、

 
练习三、

 
练习四、


 
练习五、
- 统计一个文件夹总大小

 

练习六、

 
 
 
 





![题目:方格取数[Easy]](https://img-blog.csdnimg.cn/direct/608b05b3f45f4e4bb6aab23207154da3.png)













