package com.heima.exception; public class Demo1_Exception { 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"); } }
package com.heima.exception; public class Demo2_Exception { 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"); } }
package com.heima.exception; public class Demo3_Exception { 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"); } }
A:编译期异常和运行期异常的区别java
全部的RuntimeException类及其子类的实例被称为运行时异常,其余的异常就是编译时异常程序员
编译时异常面试
package com.heima.exception; import java.io.FileInputStream; public class Demo4_Exception { /** 编译时异常也叫作未雨绸缪异常(老师本身定义的) 未雨绸缪:在作某些事情的时候要作某些准备 编译时异常:在编译某个程序的时候,有可能会有这样那样的事情发生,好比文件找不到,这样的异常就必须在编译的时候处理 若是不处理编译通不过 运行时异常:就是程序员所犯得错误,须要回来修改代码 */ public static void main(String[] args) { try { FileInputStream fis = new FileInputStream("xxx.txt"); } catch(Exception e) { } } }
package com.heima.exception; public class Demo5_Throwable { /** 案例演示 * 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默认就用这种方式处理异常 } } }
package com.heima.exception; public class Demo6_Exception { /** 案例演示 * 举例分别演示编译时异常和运行时异常的抛出 * 编译时异常的抛出必须对其进行处理 * 运行时异常的抛出能够处理也能够不处理 * @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("年龄非法"); } } }
package com.heima.exception; public class Demo7_Finally { /** 案例演示 * 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("看看我执行了吗"); } } }
package com.heima.test; public class Test1 { /** * * A:面试题1 * final,finally和finalize的区别 * final能够修饰类,不能被继承 * 修饰方法,不能被重写 * 修饰变量,只能赋值一次 * * finally是try语句中的一个语句体,不能单独使用,用来释放资源 * * finalize是一个方法,当垃圾回收器肯定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。 * B:面试题2 * 若是catch里面有return语句,请问finally的代码还会执行吗?若是会,请问是在return前仍是return后。 */ public static void main(String[] args) { Demo d = new Demo(); System.out.println(d.method()); } } class Demo { public int method() { int x = 10; try { x = 20; System.out.println(1/0); return x; } catch (Exception e) { x = 30; return x; } finally { x = 40; //return x; 千万不要在finally里面写返回语句,由于finally的做用是为了释放资源,是确定会执行的 //若是在这里面写返回语句,那么try和catch的结果都会被改变,因此这么写就是犯罪 } } }
package com.heima.exception; public class Demo8_Exception { /** 案例演示 * 自定义异常的基本使用 */ public static void main(String[] args) { } } class AgeOutOfBoundsException extends Exception { public AgeOutOfBoundsException() { super(); } public AgeOutOfBoundsException(String message) { super(message); } }
B:如何使用异常处理算法
区别:数据库
若是JDK没有提供对应的异常,须要自定义异常。数组
package com.heima.test; import java.math.BigDecimal; import java.math.BigInteger; import java.util.Scanner; public class Test2 { /** * 键盘录入一个int类型的整数,对其求二进制表现形式 * 若是录入的整数过大,给予提示,录入的整数过大请从新录入一个整数BigInteger * 若是录入的是小数,给予提示,录入的是小数,请从新录入一个整数 * 若是录入的是其余字符,给予提示,录入的是非法字符,请从新录入一个整数 * * 分析: * 1,建立键盘录入对象 * 2,将键盘录入的结果存储在String类型的字符串中,存储int类型中若是有不符合条件的直接报错,没法进行后续判断 * 3,键盘录入的结果转换成int类型的数据,是正确的仍是错误的 * 4,正确的直接转换 * 5,错误的要进行对应判断 */ public static void main(String[] args) { Scanner sc = new Scanner(System.in); System.out.println("请输入一个整数:"); while(true) { String line = sc.nextLine(); //将键盘录入的结果存储在line中 try { int num = Integer.parseInt(line); //将字符串转换为整数 System.out.println(Integer.toBinaryString(num));//将整数转换为二进制 break; //跳出循环 }catch(Exception e) { try { new BigInteger(line); System.out.println("录入错误,您录入的是一个过大整数,请从新输入一个整数:"); }catch (Exception e2) { //alt + shif + z (try catch快捷键) try { new BigDecimal(line); System.out.println("录入错误,您录入的是一个小数,请从新输入一个整数:"); } catch (Exception e1) { System.out.println("录入错误,您录入的是非法字符,请从新输入一个整数:"); } } } } } }