码迷,mamicode.com
首页 > 其他好文 > 详细

异常处理

时间:2019-08-06 00:37:35      阅读:71      评论:0      收藏:0      [点我收藏+]

标签:处理异常   throwable   turn   throws   catch   默认   常见   get   throw   

异常的继承体系
  * 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)

 

 

异常处理

标签:处理异常   throwable   turn   throws   catch   默认   常见   get   throw   

原文地址:https://www.cnblogs.com/yaobiluo/p/11306398.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!