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)

     
    竹杖芒鞋轻胜马,一蓑烟雨任平生。 回首向来萧瑟处,也无风雨也无晴。
  • 相关阅读:
    转: IOS程序内发短信 MFMessageComposeViewController
    对象之间如何比较是否相等?
    相比xib 使用代码编排view 的一个明显的好处就是可以更好地重复使用已有代码,减少代码冗余。
    关于deselectRowAtIndexPath
    IOS中为tableViewCell增加右侧标记(选中或者更多)
    ios 中是否每一个对象(尤其是在使用多线程时),都要判断一下对象是否为nil,以防止程序闪退?
    模拟出ios中流行的黑色背景底
    转 ios给view设置圆角
    转 UIAlertView 不显示、屏幕变灰
    转 UIActivityIndicatorView、UIProgressView 活动与进度指示器-IOS开发
  • 原文地址:https://www.cnblogs.com/yaobiluo/p/11306398.html
Copyright © 2011-2022 走看看