异常:在Java语言中,将程序执行中发生的不正常状况称为“异常”java
Java程序在执行过程当中所发生的异常事件可分为两类:程序员
StackOverflowError和OOM
。通常不编写针对性的代码进行处理。错误解决方法sql
遇到错误就终止程序的运行
由程序员在编写程序时,就考虑到错误的检测、错误消息的提示,以及错误的处理编程
运行时异常:是指编译器不要求强制处置的异常;通常是指编程时的逻辑错误,是程序员应该积极避免其出现的异常;java.lang.RuntimeException
类及它的子类都是运行时异常数组
编译时异常:是指编译器要求必须处置的异常;编译器要求Java程序必须捕获或声明全部编译时异常网络
java.lang.RuntimeException
ide
ClassCastException
ArrayIndexOutOfBoundsException
NullPointerException
ArithmeticException
NumberFormatException
InputMismatchException
java.io.IOExeption
测试
FileNotFoundException
EOFException
java.lang.ClassNotFoundException
this
java.lang.InterruptedException
指针
java.io.FileNotFoundException
java.sql.SQLException
public class IndexOutExp { public static void main(String[] args) { String friends[] = { "lisa", "bily", "kessy" }; for (int i = 0; i < 5; i++) { System.out.println(friends[i]); //friends[4]? } System.out.println("\nthis is the end"); } } /* 程序IndexOutExp.java编译正确,运行结果:java IndexOutExp lisa bily kessy java.lang.ArrayIndexOutOfBoundsException at Test7_1.main(Test7_1.java:5) Exception in thread "main" */
public class NullRef { int i = 1; public static void main(String[] args) { NullRef t = new NullRef(); t = null; System.out.println(t.i); } } /* 程序NullRef.java编译正确,运行结果:java NullRef java.lang.NullPointerException at NullRef.main(NullRef.java:6) Exception in thread "main" */
public class DivideZero { int x; public static void main(String[] args) { int y; DivideZero c=new DivideZero(); y=3/c.x; System.out.println("program ends ok!"); } } /* 程序DivideZero.java编译正确,运行结果:java DivideZero java.lang.ArithmeticException: / by zero at DivideZero.main(DivideZero.java:6) Exception in thread "main" */
public class Order { public static void main(String[] args) { Object obj = new Date(); Order order; order = (Order) obj; System.out.println(order); } } /* 程序Person.java编译正确,运行结果:java Person java.lang. java.lang.ClassCastException at Person.main(Person.java:5) Exception in thread "main" */
异常对象的生成
- 虚拟机自动生成:程序运行过程当中,虚拟机检测到程序发生了问题,若是在当前代码中没有找到相应的处理程序,就会在后台自动建立一个对应异常类的实例对象并抛出——自动抛出
- 由开发人员手动建立
语法:
try{ ...... //可能产生异常的代码 } catch( ExceptionName1 e ){ ...... //当产生ExceptionName1型异常时的处置措施 } catch( ExceptionName2 e ){ ...... //当产生ExceptionName2型异常时的处置措施 } finally{ ...... //不管是否发生异常,都无条件执行的语句 }//finall可写可不写
try:捕获异常的第一步是用try{…}语句块选定捕获异常的范围,将可能出现 异常的代码放在try语句块中
catch (Exceptiontype e) :在catch语句块中是对异常对象进行处理的代码。每一个try语句块能够伴随 一个或多个catch语句,用于处理可能产生的不一样类型的异常对象
捕获异常的有关信息: 与其它对象同样,能够访问一个异常对象的成员变量或调用它的方法
getMessage()
获取异常信息,返回字符串
printStackTrace()` 获取异常类名和异常信息,以及异常出如今程序中的位置;返回值void
finally:不论在try代码块中是否发生了异常事件,catch语句是否执行,catch语句是否有异常,catch语句中是否有return,finally块中的语句都会被执行
public void readFile(String file) throws FileNotFoundException{ …… // 读文件的操做可能产生FileNotFoundException类型的异常 FileInputStream fis = new FileInputStream(file); …… }
重写方法不能抛出比被重写方法范围更大的异常类型。在多态的状况下,对父类方法的调用-异常的捕获按父类声明的异常处理
RuntimeException
的子类serialVersionUID
用户自定义异常类
MyException
,用于描述数据取值范围错误信息;用户本身的异常类必须继承现有的异常类
//自定义类 class MyException extends Exception { static final long serialVersionUID = 13465653435L; private int idnumber; public MyException(String message, int id) { super(message); this.idnumber = id; } public int getId() { return idnumber; } } //测试类 public class MyExpTest { public void regist(int num) throws MyException { if (num < 0) throw new MyException("人数为负值,不合理", 3); else System.out.println("登记人数" + num); } public void manager() { try { regist(100); } catch (MyException e) { System.out.print("登记失败,出错种类" + e.getId()); } System.out.print("本次登记操做结束"); } public static void main(String args[]) { MyExpTest t = new MyExpTest(); t.manager(); } }