Java暑期学习第二十三天日报

一、今日学习的内容:

今天学习了8.3的综合实例和8.5的课后习题

二、遇到的问题:

 

三、明日计划:

明天计划学习12.1和12.2的内容。

 

今天学习的具体内容如下:

1.综合实例——不可恢复异常

import java.util.*;
public class Demol {
    public static void main (String [] args) {
        Scanner in=new Scanner(System.in);
        while(true) {
            System.out.println("1.堆溢出   2.栈溢出");
            String str=in.nextLine();
            if(str.equals("1")) {
                char []chs=new char[0x7fffffff];
                
            }
            else if(str.equals("2")) {
                f();
            }
                
            
        }
    }
    public static void f() {
        f();
    }

}

测试截图:

堆溢出:

 

 

栈溢出:

 

 

 

 

2.课后习题

(1)自己写一段程序,用throws捕获异常

public class ThrowsDemo {
    public static void main(String [] args) throws Exception{
        int x=11;
        int y=x/0;
        System.out.println(y);
        
    }
    

}

测试截图:

 

 

(2)使用throw在题目一的基础上抛出异常

public class ThrowsDemo {
    public static void main(String [] args) {
        try{
            int x=11;
            int y=x/0;
            throw new Exception();
            
        }
        catch(Exception e) {
            e.printStackTrace();
        }
            
    }
    
}

测试截图:

 

 

(3)在题目一的基础上用try...catch来捕获异常

public class ThrowsDemo {
    public static void main(String [] args) {
        try{
            int x=11;
            int y=x/0;
            System.out.println(y);
            
        }
        catch(Exception e) {
            e.printStackTrace();
        }
        finally
        {
            System.out.println("Over!");
        }
            
    }
    
}

测试截图;

 

 

 

 

posted on 2020-07-28 13:46  桑榆非晚柠月如风  阅读(73)  评论(0编辑  收藏  举报