异常:在Java语言中,将程序执行中发生的不正常状况称为“异常”。 (开发过程当中的语法错误和逻辑错误不是异常)java
Java程序在执行过程当中所发生的异常事件可分为两类:程序员
异常的体系结构 * java.lang.Throwable * |-----java.lang.Error:通常不编写针对性的代码进行处理。 * |-----java.lang.Exception:能够进行异常的处理 * |------编译时异常(checked)不会生成字节码文件 * |-----IOException * |-----FileNotFoundException * |-----ClassNotFoundException * |------运行时异常(unchecked,RuntimeException) * |-----NullPointerException//空指针异常 * |-----ArrayIndexOutOfBoundsException//数组角标越界 * |-----ClassCastException//类型转化异常 * |-----NumberFormatException//编码格式异常 * |-----InputMismatchException//输入不匹配 * |-----ArithmeticException//算术异常
java中异常类的继承关系面试
//******************如下是运行时异常*************************** //ArithmeticException @Test public void test6(){ int a = 10; int b = 0; System.out.println(a / b); } //InputMismatchException @Test public void test5(){ Scanner scanner = new Scanner(System.in); int score = scanner.nextInt(); System.out.println(score); scanner.close(); } //NumberFormatException @Test public void test4(){ String str = "123"; str = "abc"; int num = Integer.parseInt(str); } //ClassCastException @Test public void test3(){ Object obj = new Date(); String str = (String)obj; } //IndexOutOfBoundsException @Test public void test2(){ //ArrayIndexOutOfBoundsException // int[] arr = new int[10]; // System.out.println(arr[10]); //StringIndexOutOfBoundsException String str = "abc"; System.out.println(str.charAt(3)); } //NullPointerException @Test public void test1(){ // int[] arr = null; // System.out.println(arr[3]); String str = "abc"; str = null; System.out.println(str.charAt(0)); } //******************如下是编译时异常*************************** @Test public void test7(){ // File file = new File("hello.txt"); // FileInputStream fis = new FileInputStream(file); // // int data = fis.read(); // while(data != -1){ // System.out.print((char)data); // data = fis.read(); // } // // fis.close(); }
过程一:"抛":程序在正常执行的过程当中,一旦出现异常,就会在异常代码处生成一个对应异常类的对象。并将此对象抛出。一旦抛出对象之后,其后的代码就再也不执行。数据库
关于异常对象的产生:编程
① 系统自动生成的异常对象数组
② 手动的生成一个异常对象,并抛出(throw)网络
过程二:"抓":能够理解为异常的处理方式:① try-catch-finally ② throwside
try{ //可能出现异常的代码 }catch(异常类型1 变量名1){ //处理异常的方式1 }catch(异常类型2 变量名2){ //处理异常的方式2 }catch(异常类型3 变量名3){ //处理异常的方式3 } .... finally{ //必定会执行的代码 }
说明:this
finally是可选的。编码
使用try将可能出现异常代码包装起来,在执行过程当中,一旦出现异常,就会生成一个对应异常类的对象,根据此对象的类型,去catch中进行匹配
一旦try中的异常对象匹配到某一个catch时,就进入catch中进行异常的处理。一旦处理完成,就跳出当前的try-catch结构(在没写finally的状况。继续执行其后的代码)
catch中的异常类型若是没子父类关系,则谁声明在上,谁声明在下无所谓。
catch中的异常类型若是知足子父类关系,则要求子类必定声明在父类的上面。不然,报错
经常使用的异常对象处理的方式: ① String getMessage() ② printStackTrace()
在try结构中声明的变量,再出了try结构之后,就不能再被调用
try-catch-finally结构能够嵌套
如何看待代码中的编译时异常和运行时异常?
"throws + 异常类型"==写在方法的声明处==。指明此方法执行时,可能会抛出的异常类型。 一旦当方法体执行时,出现异常,仍会在异常代码处生成一个异常类的对象,此对象知足throws后异常类型时,就会被抛出。异常代码后续的代码,就再也不执行!
try-catch-finally:真正的将异常给处理掉了。 throws的方式只是将异常抛给了方法的调用者。==并没真正将异常处理掉==。
补充: 方法重写的规则之一: 子类重写的方法抛出的异常类型不大于父类被重写的方法抛出的异常类型
在程序执行中,除了自动抛出异常对象的状况以外,咱们还能够手动的throw一个异常类的对象。
throw 和 throws区别: throw 表示抛出一个异常类的对象,生成异常对象的过程。声明在方法体内。 throws 属于异常处理的一种方式,声明在方法的声明处。
class Student{ private int id; public void regist(int id) throws Exception { if(id > 0){ this.id = id; }else{ //手动抛出异常对象 // throw new RuntimeException("您输入的数据非法!"); // throw new Exception("您输入的数据非法!"); throw new MyException("不能输入负数"); } } @Override public String toString() { return "Student [id=" + id + "]"; } }
public class MyException extends Exception{ static final long serialVersionUID = -7034897193246939L; public MyException(){ } public MyException(String msg){ super(msg); } }