• 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!");
            }
                
        }
        
    }

    测试截图;

     

  • 相关阅读:
    jsp页面中使用 splitfn:split注意事项
    【SQL】- 基础知识梳理(二)
    【SQL】- 基础知识梳理(一)
    面向对象编程思想-解释器模式
    NPOI操作Excel
    面向对象编程思想-备忘录模式
    面向对象编程思想-访问者模式
    面向对象编程思想-责任链模式
    面向对象编程思想-策略模式
    面向对象编程思想-状态模式
  • 原文地址:https://www.cnblogs.com/Lizhichengweidashen/p/13390680.html
Copyright © 2020-2023  润新知