zoukankan      html  css  js  c++  java
  • 异常处理

    异常的继承体系
      * Throwable
        * Error         服务器宕机,数据库崩溃等
        * Exception
          * RuntimeException

    Java中的异常被分为两大类:编译时异常和运行时异常。
      * 所有的RuntimeException类及其子类的实例被称为运行时异常,其他的异常就是编译时异常 

    public static void main(String[] args) {
            //demo1();
            Demo2 d = new Demo2();
            try{
                int x = d.div(10, 0);     // ArithmeticException
                System.out.println(x); 
            }
            catch(ArithmeticException a){
                System.out.println("除数出错了,除数为0了");
            }
    public static void main(String[] args) {
            int a = 10;
            int b = 0;
            int[] arr = {11,22,33,44,55};
            
            try {
                System.out.println(a/b);
                System.out.println(arr[10]);
                arr = null;
                System.out.println(arr[0]);
            } catch (ArithmeticException e) {
                System.out.println("除数不能为0");
            }catch (ArrayIndexOutOfBoundsException e) {
                System.out.println("索引越界了");
            }catch (Exception e) {
                System.out.println("出错了");
            }
        }
    
    }

    A:Throwable的几个常见方法
      * a:getMessage()
        * 获取异常信息,返回字符串。
      * b:toString()
        * 获取异常类名和异常信息,返回字符串。
      * c:printStackTrace()
        * 获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。
        jvm 默认就是调用这个

    public class demon5_throwable {
        /*
         * A:Throwable的几个常见方法
        * a:getMessage()
            * 获取异常信息,返回字符串。
        * b:toString()
            * 获取异常类名和异常信息,返回字符串。
        * c:printStackTrace()
            * 获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。
         */
        public static void main(String[] args) {
            try {
                System.out.println(1/0);
            } catch (Exception e) {
                System.out.println(e.getMessage());
            }
        }
    }


    throws 方式处理异常, 一抛到底,一级抛一级

    public class demon6_exception {
    
        public static void main(String[] args)   {
            Person p = new Person();
            p.setAge(20);
            System.out.println(p.getAge());
        }
    }
     
    class Person{
        private String name;
        private int age;
        public Person() {
            super();    
        }
        public Person(String name, int age) {
            super();
            this.name = name;
            this.age = age;
        }
        public String getName() {
            return name;
        }
        public void setName(String name) {
            this.name = name;
        }
        public int getAge() {
            return age;
        }
        public void setAge(int age) {
            if (this.age>0 && this.age < 150) {
                this.age = age;
            } else {
                throw new RuntimeException("年龄非法");
            }
        }    
    }

    Exception in thread "main" java.lang.RuntimeException: 年龄非法
    at com.heima.trycatch.Person.setAge(demon6_exception.java:38)
    at com.heima.trycatch.demon6_exception.main(demon6_exception.java:9)

     
    竹杖芒鞋轻胜马,一蓑烟雨任平生。 回首向来萧瑟处,也无风雨也无晴。
  • 相关阅读:
    RecyclerView 数据刷新的几种方式 局部刷新 notify MD
    【图片】批量获取几万张图片
    RV BaseRecyclerViewAdapterHelper 总结 MD
    RecyclerView.ItemDecoration 间隔线
    Kotlin【简介】Android开发 配置 扩展
    Kotlin 特性 语法糖 优势 扩展 高阶 MD
    一个十分简洁实用的MD风格的UI主框架
    折叠伸缩工具栏 CollapsingToolbarLayout
    FloatingActionButton FAB 悬浮按钮
    Glide Picasso Fresco UIL 图片框架 缓存 MD
  • 原文地址:https://www.cnblogs.com/yaobiluo/p/11306398.html
Copyright © 2011-2022 走看看