异常

来源:互联网 发布:成都机场附近住宿 知乎 编辑:程序博客网 时间:2024/06/05 10:32

public class Demo1_Exception {

/** * * A:JVM默认是如何处理异常的    * main函数收到这个问题时,有两种处理方式:    * a:自己将该问题处理,然后继续运行    * b:自己没有针对的处理方式,只有交给调用main的jvm来处理    * jvm有一个默认的异常处理机制,就将该异常进行处理.    * 并将该异常的名称,异常的信息.异常出现的位置打印在了控制台上,同时将程序停止运行* B:案例演示    * JVM默认如何处理异常 */public static void main(String[] args) {    //demo1();    Demo d = new Demo();    int x = d.div(10, 0);    System.out.println(x);}public static void demo1() {    int[] arr = {11,22,33,44,55};    //arr = null;                   //NullPointerException              空指针异常    System.out.println(arr[10]);    //ArrayIndexOutOfBoundsException    数组索引越界异常}

}

class Demo {
/*
* 除法运算
*/
public int div(int a,int b) { //a = 10,b = 0
return a / b; // 10 / 0 被除数是10,除数是0当除数是0的时候违背了算数运算法则,抛出异常
//new ArithmeticException(“/ by zero”);
}
}

public class Demo2_Exception {

/** * * A:异常处理的两种方式        * a:try…catch…finally            * try catch            * try catch finally            * try finally         * b:throws    * B:try...catch处理异常的基本格式        * try…catch…finally    * C:案例演示        * try...catch的方式处理1个异常    try:用来检测异常的    catch:用来捕获异常的    finally:释放资源    世界上最真情的相依就是你在try我在catch,无论你发神马脾气,我都静静接受,默默处理    当通过trycatch将问题处理了,程序会继续执行 */public static void main(String[] args) {    Demo2 d = new Demo2();    try{        int x = d.div(10, 0);        System.out.println(x);    }catch(ArithmeticException a) {     //ArithmeticException a = new ArithmeticException();        System.out.println("出错了,除数为零了");    }    System.out.println("1111111111111111");}

}

class Demo2 {
/*
* 除法运算
*/
public int div(int a,int b) { //a = 10,b = 0
return a / b; // 10 / 0 被除数是10,除数是0当除数是0的时候违背了算数运算法则,抛出异常
//new ArithmeticException(“/ by zero”);
}
}

public class Demo3_Exception {

/** * * A:案例演示 * try...catch的方式处理多个异常 * JDK7以后处理多个异常的方式及注意事项 *  * 安卓,客户端开发,如何处理异常?try{}catch(Exception e){} * ee,服务端开发,一般都是底层开发,从底层向上抛 *  * try后面如果跟多个catch,那么小的异常放前面,大的异常放后面,根据多态的原理,如果大的放前面,就会将所有的子类对象接收 * 后面的catch就没有意义了 */public static void main(String[] args) {    //demo1();    int a = 10;    int b = 0;    int[] arr = {11,22,33,44,55};    //JDK7如何处理多个异常    try {        System.out.println(a / b);        System.out.println(arr[10]);    } catch (ArithmeticException | ArrayIndexOutOfBoundsException e) {        System.out.println("出错了");    } }public static void demo1() {    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("除数不能为零");    } catch (ArrayIndexOutOfBoundsException e) {        System.out.println("索引越界了");    } catch (Exception e) {             //Exception e = new NullPointerException();        System.out.println("出错了");    }    System.out.println("over");}

}

public class Demo4_Exception {

/** * * A:编译期异常和运行期异常的区别    * Java中的异常被分为两大类:编译时异常和运行时异常。    * 所有的RuntimeException类及其子类的实例被称为运行时异常,其他的异常就是编译时异常    * 编译时异常        * Java程序必须显示处理,否则程序就会发生错误,无法通过编译    * 运行时异常        * 无需显示处理,也可以和编译时异常一样处理* B:案例演示    * 编译期异常和运行期异常的区别编译时异常也叫做未雨绸缪异常(老师自己定义的)    未雨绸缪:在做某些事情的时候要做某些准备    编译时异常:在编译某个程序的时候,有可能会有这样那样的事情发生,比如文件找不到,这样的异常就必须在编译的时候处理    如果不处理编译通不过    运行时异常:就是程序员所犯得错误,需要回来修改代码 */public static void main(String[] args) {    try {        FileInputStream fis = new FileInputStream("xxx.txt");    } catch(Exception e) {    }}

}

public class Demo5_Throwable {

/** * * A:Throwable的几个常见方法        * a:getMessage()            * 获取异常信息,返回字符串。        * b:toString()            * 获取异常类名和异常信息,返回字符串。        * c:printStackTrace()            * 获取异常类名和异常信息,以及异常出现在程序中的位置。返回值void。    * B:案例演示        * Throwable的几个常见方法的基本使用 */public static void main(String[] args) {    try {        System.out.println(1/0);    } catch (Exception e) {         //Exception e = new ArithmeticException("/ by zero");        //System.out.println(e.getMessage());       //获取异常信息        //System.out.println(e);        //调用toString方法,打印异常类名和异常信息        e.printStackTrace();        //jvm默认就用这种方式处理异常    }}

}

public class Demo6_Exception {

/** * * A:throws的方式处理异常        * 定义功能方法时,需要把出现的问题暴露出来让调用者去处理。        * 那么就通过throws在方法上标识。    * B:案例演示        * 举例分别演示编译时异常和运行时异常的抛出        * 编译时异常的抛出必须对其进行处理        * 运行时异常的抛出可以处理也可以不处理 * @throws Exception  */public static void main(String[] args) throws Exception {    Person p = new Person();    p.setAge(-17);    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) throws AgeOutOfBoundsException {    if(age >0 && age <= 150) {        this.age = age;    }else {        //Exception e = new Exception("年龄非法");        //throw e;        throw new AgeOutOfBoundsException("年龄非法");    }}   

}

public class Demo7_Finally {

/** * * A:finally的特点        * 被finally控制的语句体一定会执行        * 特殊情况:在执行到finally之前jvm退出了(比如System.exit(0))    * B:finally的作用        * 用于释放资源,在IO流操作和数据库操作中会见到    * C:案例演示        * finally关键字的特点及作用    *return语句相当于是方法的最后一口气,那么在他将死之前会看一看有没有finally帮其完成遗愿,如果有就将finally执行    *后在彻底返回 */public static void main(String[] args) {    try {        System.out.println(10/0);    } catch (Exception e) {        System.out.println("除数为零了");        System.exit(0);                             //退出jvm虚拟机        return;    } finally {        System.out.println("看看我执行了吗");    }}

}

public class Demo8_Exception {

/** * * A:为什么需要自定义异常        * 通过名字区分到底是神马异常,有针对的解决办法         * 举例:人的年龄    * B:自定义异常概述        * 继承自Exception        * 继承自RuntimeException    * C:案例演示        * 自定义异常的基本使用 */public static void main(String[] args) {}

}

class AgeOutOfBoundsException extends Exception {

public AgeOutOfBoundsException() {    super();}public AgeOutOfBoundsException(String message) {    super(message);}

}

原创粉丝点击