• Java异常


    异常父子关系

    package ch10;
    /**
     * Created by Jiqing on 2016/11/30.
     */
    public class DivTest {
        public static void main(String[] args) {
            try {
                int a = Integer.parseInt(args[0]);
                int b = Integer.parseInt(args[1]);
                int c = a/b;
                System.out.println("您输入的两个数相除的结果是:"+c);
            } catch (IndexOutOfBoundsException ie) {
                System.out.println("数组越界:运行程序输入的参数个数不够");
            } catch (NumberFormatException ne) {
                System.out.println("数字格式异常:程序只能接收整数参数");
            } catch (ArithmeticException ae) {
                System.out.println("算术异常");
            } catch (Exception e) {
                System.out.println("未知异常");
            }
        }
    }
    
    // 如果运行该程序输入的参数不够,将会发生数组越界异常,用IndexOutOfBoundsException
    // 如果运行该程序输入的参数不是数字,而是字母,将发生数字格式异常,用NumberFormatException
    // 如果该程序输入的第二个参数是0,将发生除0异常,用ArithmeticException
    // 如果出现其他异常,用Exception
    
    

    牢记并掌握上门三种常见的运算异常。

    捕获多个异常

    package ch10;
    
    /**
     * Created by Jiqing on 2016/11/30.
     */
    public class MultiExceptionTest {
        // 同时捕获多个异常
        public static void main(String[] args) {
            try {
                int a = Integer.parseInt(args[0]);
                int b = Integer.parseInt(args[1]);
                int c = a/b;
                System.out.println("您输入的两个数相除的结果是:"+c);
            } catch (IndexOutOfBoundsException | NumberFormatException | ArithmeticException ie) {
                    System.out.println("程序发生了数组越界、数字格式异常、算术异常之一");
                    // 捕获多异常,异常变量有final修饰
            } catch (Exception e) {
                    System.out.println("未知异常");
                    // 捕获单异常,异常变量没有final修饰
            }
        }
    }
    
    

    抛出异常throws

    package ch10;
    
    import java.io.FileInputStream;
    import java.io.IOException;
    
    /**
     * Created by Jiqing on 2016/11/30.
     */
    public class ThrowsTest {
        // 一旦使用throws抛出异常,程序就无法使用try catch来捕获该异常了
        public static void main(String[] args)
            throws IOException
        {
            FileInputStream fis = new FileInputStream("a.txt");
        }
    }
    
    

    自行抛出异常throw

    package ch10;
    
    /**
     * Created by Jiqing on 2016/12/1.
     */
    public class ThrowTest {
        public static void main(String[] args)
        throws Exception // 抛出异常
        {
            // 自行抛出异常
            throw new Exception("异常了");
        }
    }
    
    

    Java异常跟踪栈

    package ch10;
    
    import ch6.Enum.SeasonEnum;
    
    /**
     * Created by Jiqing on 2016/12/1.
     */
    class SelfException extends RuntimeException {
        SelfException() {}
        SelfException(String msg) {
            super(msg);
        }
    }
    
    public class PrintStackTraceTest {
        // Java异常跟踪栈
        // 异常从发生异常的方法逐渐向外传播,首先是调用者,然后传给其调用者,直至main方法,如果main方法异常没有处理,JVM会中止该程序,并打印异常跟踪栈信息
        // 栈,队列  队列先进先出,栈先进后出
    
        public static void main(String[] args) {
            firstMethod();
        }
    
        public static void firstMethod() {
            secondMethod();
        }
    
        public static void secondMethod() {
            thirdMethod();
        }
    
        public static void thirdMethod() {
            throw new SelfException("自定义异常信息");
        }
    
    //    Exception in thread "main" ch10.SelfException: 自定义异常信息
    //    at ch10.PrintStackTraceTest.thirdMethod(PrintStackTraceTest.java:29)
    //    at ch10.PrintStackTraceTest.secondMethod(PrintStackTraceTest.java:25)
    //    at ch10.PrintStackTraceTest.firstMethod(PrintStackTraceTest.java:21)
    //    at ch10.PrintStackTraceTest.main(PrintStackTraceTest.java:17)
    }
    
    
  • 相关阅读:
    翻硬币
    排队打水问题
    连续邮资问题
    Linux-AWK命令
    SpringInAction第五章总结 使用配置属性
    SpringInAction 第四章笔记 保护Spring
    SpringInAction第三章笔记 --使用数据
    SpringInAction第二章笔记
    SpringInAction第一章笔记
    SpringBoot启动任务
  • 原文地址:https://www.cnblogs.com/jiqing9006/p/6120225.html
Copyright © 2020-2023  润新知